在Java文件中给一个控件动态设置属性
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white">
<TextView
android:id="@+id/txt_hour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignBottom="@id/txt_title"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/txt_title1" />
</
RelativeLayout
>
给textview 设置相对的属性 Java文件中对应的
创建设置属性控件的父布局的 LayoutParams
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_title);//设置 android:layout_alignBottom="@id/txt_title"
layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.txt_title1);//设置android:layout_toRightOf="@id/txt_title1"
layoutParams.setMargins(UIUtils.dp2px(mContext, 5), 0, 0, 0);设置marginleft
txt_hour.setLayoutParams(layoutParams);