有些时候我们需要动态的设置-某些布局的位置,(也是代码适配)
一:父布局是,线性布局:
xml文件如下
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id_about_data_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lineSpacingMultiplier="1.1"
android:text="省钱。"
android:textColor="@color/color_666666"
android:textSize="@dimen/text_font_14" />
</LinearLayout>
代码设置,因为父布局是一个线性布局,所以我们直接new 一个线性布局的参数,然后在给当前需要改变位置的空间设置最新的参数(相当于xml中设置)
*代码如下
LinearLayout.LayoutParams lp_4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp_4.leftMargin = needLeftMargn;
lp_4.topMargin = 12;
id_about_data_4.setLayoutParams(lp_4);
*如此动态设置完成
二:父布局是,相对布局
xml布局如下
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id_tv_top_captrue_top_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lineSpacingMultiplier="1.1"
android:text="省钱。"
android:textColor="@color/color_666666"
android:textSize="@dimen/text_font_14" />
</RelativeLayout>
动态设置代码
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(70));//工具类哦
layoutParams.topMargin = topHeight;
//这个api设置相对布局,位置的 params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
id_tv_top_captrue_top_1.setLayoutParams(layoutParams);
*如此动态设置完成