【问题标题】:Creating a button programatically in OnCreateView (Fragment class)在 OnCreateView (片段类)中以编程方式创建按钮
【发布时间】: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中有3 LinearLayout(s),那么你说的是哪一个呢?
  • 1) 我想以编程方式向 xml 添加一个按钮。 2)我说的是内部(第二)线性布局。
  • 如果您固定了按钮的数量,那么您可以在布局中设计这些按钮并设置 visibility=gone。当您需要这些按钮时,请更改 visibility=visible。

标签: java android xml button android-fragments


【解决方案1】:

您是否尝试实用地重复以下布局?

               <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>

然后使用ListView

1) 像这样修改你的主 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ListView>

</LinearLayout>

2)新建一个xml文件并粘贴

                   <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>

3) 创建Adapter 填充ListView

【讨论】:

  • 是的,布局重复了。
  • 我想在我的 Fragment 类中动态创建按钮。我在 Fragment 课程中有一个条件。如果满足,则将创建按钮。我不能让它保持静止。
【解决方案2】:

您将从这里获得解决问题的帮助:

ArrayList<String> mList = new ArrayList<String>();

mList.add("Button 0");
mList.add("Button 1");
mList.add("Button 2");
// this has added 3 buttons

ListAdapter mAdapter = new ListAdaper(getApplicationContext, mList);

if( /* your condition */ ) {
    mList.add("Button "+i);
}

【讨论】:

    【解决方案3】:

    为您的 LinearLayout 添加一个 id:

    <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"  
                        android:id="@+id/myLinearLayout"><!-- i guess this is your LinearLayout-->
    
                        <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>
    

    然后来自java代码:

      Button myButton = new Button(YourActivity.this);
        myButton.setText("new button");
    
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(width, height, weight);
        myButton.setLayoutParams(param);
    
        myButton.setBackgroundResource(YourResource); // like "android:src"
        myButton.setBackground(Drawable); // like "android:background"
    
       LinearLayout ll = (LinearLayout)findViewById(R.id.myLinearLayout);
    
        ll.addView(myButton, param);
    

    【讨论】:

    • 我明白了。但我仍然无法获得如何设置按钮的参数,如 layout_weight、layout_height、android:background、android:src ??
    • 谢谢... layout_weight 呢?
    • weight 字段 = layout_weight
    • 抱歉回复晚了。但是代码中似乎没有错误。甚至没有运行时错误。然而,按钮似乎没有正确形成。我只看到一条有一定宽度的红色(我在按钮上设置的颜色)线。
    • 可以发截图吗?也许你的按钮配置有问题(重量、宽度的值......)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2012-06-19
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多