ViewGroup.LayoutParams params 在网上找了很多教程。按照如下方法写:
<strong>LinearLayout.LayoutParams imagebtn_params = newLinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imagebtn_params.height = dm.widthPixels / 4;
imagebtn_params.width = dm.widthPixels / 4;
imagebtn.setLayoutParams(imagebtn_params);</strong>
这里参见
http://blog.csdn.net/pathuang68/article/details/6647716
但是我发现View的高度并没有改变。
而我的布局文件比之是更加复杂的,如下
<strong><TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/SheduleTitle"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@android:color/black">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="9dp"
android:visibility="gone" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/SheduleGroup"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SheduleButton"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SheduleMore"
android:background="@android:color/white"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SheduleImfor"
android:background="@android:color/black"
/>
</RadioGroup>
</FrameLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" >
</FrameLayout>
</TabHost>
</strong>
我现在想改变的是FrameLayout的高度。
如这里参考的,我发现是 setContentView(R.id.xx),而官方文档 setContentView(View view),setContentView(View view,
ViewGroup.LayoutParams
params)。
在 inflater 获得整个View以后,我再用前面所参考的方法设置FrameLayout高度。
使用setContent(View view)方法绘图,成功改变了高度。
简单原因 : R.id.xx 是我们已经写好的xml,其中一些控件属性其已经固定不变了,而View相当于一个中间变量,我们可以改变其中的参数。