【发布时间】:2014-12-28 20:23:35
【问题描述】:
在我的Activity 的onCreate 方法中,我知道我想要在col0 中的Buttons 的数量。在以下示例中,它是四个 Buttons。然后我的TextViewv0 将android:layout_weight 设置为number of buttons minus one(这是因为v1 应该与所有Buttons 具有相同的高度)。与其为每个可能的按钮数量提供一个自己的 xml 文件,不如将其概括为更好的方法,以便动态 java 代码以某种方式生成适当的布局。这怎么可能?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/col0"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical">
<Button
android:id="@+id/b0"
style="@style/MyButton" />
<Button
android:id="@+id/b1"
style="@style/MyButton" />
<Button
android:id="@+id/b2"
style="@style/MyButton" />
<Button
android:id="@+id/b3"
style="@style/MyButton" />
</LinearLayout>
<LinearLayout
android:id="@+id/col1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="6"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:id="@+id/v0"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3" />
<TextView
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
您不能以编程方式编辑权重吗?
-
我认为问题出在
Buttons的生成部分。 -
为什么我的问题被否决了?似乎没有人有合适的解决方案。
标签: java android xml android-layout layout