【发布时间】:2015-03-18 11:10:07
【问题描述】:
我正在尝试在我的片段类中以编程方式在 android 中创建一个按钮。然而,经过数小时的搜索,我找不到合适的解决方案。 这是我的 xml 代码:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="77dp"
android:layout_weight="1.70"
android:background="#FFCDD2"
android:src="@drawable/airtel_round_s" />
<Button
android:id="@+id/airtelamt"
android:layout_width="188dp"
android:layout_height="77dp"
android:layout_weight="0.45"
android:background="#FFCDD2"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/iciciBtn"
android:layout_width="wrap_content"
android:layout_height="77dp"
android:layout_weight="1.70"
android:background="#FFE0B2"
android:src="@drawable/icici_logo_s" />
<Button
android:id="@+id/iciciamt"
android:layout_width="188dp"
android:layout_height="77dp"
android:layout_weight="0.45"
android:background="#FFE0B2"
android:text="Button" />
</LinearLayout>
</LinearLayout>
有人可以指导我吗??
这是应用程序的屏幕截图:
【问题讨论】:
-
你想在哪里添加按钮(在哪个布局中)?
-
线性布局内。层次结构必须与上面 xml 中定义的相同。
-
1) 您想创建一个
Button,每次单击它时以编程方式加载此xml,或者您想以编程方式在此xml 中添加Button? --- 2)第二种情况,你的xml中有3LinearLayout(s),那么你说的是哪一个呢? -
1) 我想以编程方式向 xml 添加一个按钮。 2)我说的是内部(第二)线性布局。
-
如果您固定了按钮的数量,那么您可以在布局中设计这些按钮并设置 visibility=gone。当您需要这些按钮时,请更改 visibility=visible。
标签: java android xml button android-fragments