我需要根据从配置文件加载的信息动态地(以编程方式)调整应用程序最上面的布局。
现在我拥有的in main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/background">
</LinearLayout>
所以我正在寻找一种改变android的方法:通过编程改变布局宽度和布局高度。
任何帮助都将不胜感激
室
编辑:
根据我的建议,我尝试了以下几点:
LinearLayout l2 = (LinearLayout)findViewById(R.id.LinearLayout07);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT,1);
l2.setLayoutParams(params);
但在运行时会出现以下错误:
Thread [<3> main] (Suspended (exception ClassCastException))
FrameLayout.onLayout(boolean, int, int, int, int) line: 288
FrameLayout(View).layout(int, int, int, int) line: 6133
....
最佳答案
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT,1);
layout.setLayoutParams(params);
其中布局是用findviewbyid获取的线性布局。宽度和高度的参数是用权重1填充父项。
当然,您可以使用以像素为单位的固定大小来代替填充父选项,如果不需要,也可以省略权重。