如何动态设置图像视图右侧的文本视图

时间:2016-02-25 14:01:36

标签: android dynamic view textview imageview

我有一个动态事件视图。在那我有图像视图和文本视图。我想在通知中显示图像视图不为空,在图像视图右侧显示文本视图。

为此,我将图像视图的可见性设置为已消失。因此,如果通知不是空的,则图像视图应该是可见的,文本视图应该移动到图像视图的右侧。

如何实现这一目标?

事件视图布局:

    <?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);

        }
    });
}

现在图像视图显示在文本视图上方。如果文本视图可见,如何将文本视图右移到图像视图?

2 个答案:

答案 0 :(得分:1)

只需在textview中添加此属性

即可
android:layout_toRightOf="@+id/notify"

答案 1 :(得分:0)

这样做:

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