在代码中更改relativelayoout的layout_weight

时间:2013-01-25 21:50:53

标签: android

我的xml文件中有以下布局。我的xml中还有其他类似的RelativeLayouts

    <RelativeLayout
    android:id="@+id/contentTextViewLayout"
    android:layout_weight="0.9"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/textViewStory1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:lineSpacingMultiplier="1.2"
        android:textColor="#000000"
        android:background="@drawable/book"/>
</RelativeLayout>

我想更改代码中layout_weight的{​​{1}}参数。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

请参阅第一个问题的评论:

Set the layout weight of a TextView programmatically

将您的relativelayout替换为textview。

编辑,拼写全部:

首先给你的外部LinearLayout充气:

    LayoutInflater inflater = LayoutInflater.from(context);
    LinearLayout ll = (LinearLayout)
        inflater.inflate(R.layout.some_linear_layout, null);
    // now get a reference to your relativelayout inside it
    RelativeLayout yourRL = (RelativeLayout)
        ll.findViewById(that_relative_layout);

    // parameters are width, height, weight
    yourRL.setLayoutParams(new 
        LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 1f));