访问代码中的权重属性

时间:2013-01-09 22:17:14

标签: android

我的布局xml文件中有以下代码

 <RelativeLayout
    android:id="@+id/contentTextViewLayout"
    android:layout_weight="9"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

如何在代码中获取RelativeLayout的layout_weight属性值?

2 个答案:

答案 0 :(得分:0)

为什么不只是获取相对布局的宽度,然后将edittext的宽度设置为布局宽度的百分比。

int i = relativelayout.getWidth();
int x = //any number, depending on how wide you what your text view
textview.setWidth(i/x);

答案 1 :(得分:0)

您必须使用RelativeLayout.LayoutParams

尝试以下方法:

RelativeLayout r = ((RelativeLayout)(findViewById(R.id.contentTextViewLayout))); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
r.setLayoutParams(params);

params.weight = 1.0f可以根据您的需要进行更改。

相关问题