2016-02-25 82 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); 
相关问题