2016-02-25 81 views
0

我有一個動態事件視圖。在那裏我有圖像視圖和文本視圖。我想在通知中顯示圖像視圖不是空的,文本視圖在圖像視圖的右側。如何動態設置圖片視圖的文本視圖

爲此,我已將圖像視圖的可見性設置爲消失。因此,如果通知不是空圖像視圖應該是可見的,文本視圖應該移動到圖像視圖的右邊。

如何實現這一目標?

事件視圖佈局:

<?xml version="1.0" encoding="utf-8"?> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/shape" 
     android:id="@+id/container" 
     android:layout_marginLeft="60dp" 
     android:layout_marginRight="12dp"> 

    <ImageView 
     android:layout_width="15dp" 
     android:layout_height="15dp" 
     android:id="@+id/notify" 
     android:layout_alignParentRight="false" 
     android:layout_alignParentEnd="false" 
     android:background="@drawable/sound" 
     android:layout_marginLeft="05dp" 
     android:layout_marginTop="04dp" 
     android:layout_marginRight="05dp" 
     android:visibility="gone" /> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
     android:id="@+id/textViewTitle" 
     android:layout_marginTop="01dp" 
     android:layout_marginLeft="05dp" /> 

</RelativeLayout> 

事件視圖的代碼:

private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,final int id,int color,String notification) { 

    eventView = inflater.inflate(R.layout.event_view, dayplanView, false); 
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams(); 

    container = (RelativeLayout) eventView.findViewById(R.id.container); 
    TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle); 
    list.add(eventView); 

    ImageView notify = (ImageView) eventView.findViewById(R.id.notify); 


    ((GradientDrawable) eventView.getBackground()).setColor(color); 

    tvTitle.setTextColor(Color.parseColor("#FFFFFF")); 


    if (tvTitle.getParent() != null) 
     ((ViewGroup) tvTitle.getParent()).removeView(tvTitle); 

    if(notification == null) 
    { 

     notify.setVisibility(View.VISIBLE); 
    } 
    else { 
     notify.setVisibility(View.GONE); 
    } 


    if(location.equals("")) 
    { 
     tvTitle.setText("Event : " + title); 
    } else { 
     tvTitle.setText("Event : " + title + " (At : " + location +")"); 
    } 
    int distance = (toMinutes - fromMinutes); 
    layoutParams.topMargin = dpToPixels(fromMinutes + 9); 
    layoutParams.height = dpToPixels(distance); 

    eventView.setLayoutParams(layoutParams); 
    dayplanView.addView(eventView); 
    container.addView(tvTitle); 



    eventView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      i = new Intent(getActivity(), AddEventActivity.class); 
      editMode = true; 
      i.putExtra("EditMode", editMode); 
      i.putExtra("id", id); 
      startActivityForResult(i, 1); 

     } 
    }); 
} 

現在圖像視圖文本視圖如上所示。如果將文本視圖右移到圖像視圖(如果它可見)?

+0

你不需要的TextView設置爲正確的動態...看到我的回答波紋管......接受並喜歡它如果它有效。 –

回答

1

只是在你的TextView添加此屬性

android:layout_toRightOf="@+id/notify" 
0

這樣做:

 ImageView notify = new ImageView(getContext()); 
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) notify.getLayoutParams(); 
     params.addRule(RelativeLayout.RIGHT_OF, R.id.notify); 
相關問題