Java代码使用LayoutParams改变RelativeLayout 中的控件布局
2015-05-15 09:14
405 查看
</pre><pre name="code" class="html"><RelativeLayout android:id="@+id/pull_to_refresh_horizontal_layout" android:layout_width="match_parent" android:layout_height="wrap_content" > <FrameLayout android:id="@+id/pull_to_refresh_image_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" > <ImageView android:id="@+id/pull_to_refresh_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="centerInside" android:src="@drawable/homepage_icon_small" /> <ProgressBar android:id="@+id/pull_to_refresh_progress" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:indeterminate="true" android:visibility="gone" /> </FrameLayout>
</RelativeLayout>
有时候在设计布局时, 需要动态修改某个控件的位置, 居中, 靠左,等,
比如:我需要用Java代码 动态改变 FrameLayout 在 RelativeLayout 中为居中状态,
代码如下:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
mHeaderImageLayout = (FrameLayout)mInnerLayout.findViewById(R.id.pull_to_refresh_image_layout);
mHeaderImageLayout.setLayoutParams(layoutParams);
mHeaderImageLayout 需要设置Layoutparams, 但需要的类型应该相对的为RelativeLayout.LayoutParams
不能使用FrameLayout .LayoutParams ,否则会出现类型错误.
相关文章推荐
- 代码中动态改变布局属性RelativeLayout.LayoutParams.addRule()
- Android 动态改变布局属性RelativeLayout.LayoutParams.addRule()
- 使用java代码构建android布局控件
- Android中不使用布局文件,java代码动态创建布局及控件
- 代码改布局的高度等Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
- android 的Java代码中的布局相关方法LayoutParams
- android 使用代码实现 RelativeLayout布局
- Java代码动态修改 ConstraintLayout 内控件布局的辅助类
- android使用java代码创建控件、布局
- android在java代码中动态添加组件及相关布局方法(LayoutParams)
- 使用Java动态设置官方百分比布局PercentRelativeLayout的heightPerce
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 纯代码RelativeLayout实现右上角布局
- Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局
- 在代码中设置RelativeLayout布局中标签的android:layout_toLeftOf、android:layout_toRightOf等属性
- 通过RelativeLayout.LayoutParams.addRule()方法在代码中设置RelativeLayout相关属性
- iOS Autolayout 自动布局代码使用规则
- 代码布局relativeLayout的位置设置