【问题标题】:Add Button to XML Layout programatically以编程方式将按钮添加到 XML 布局
【发布时间】:2013-05-03 15:40:13
【问题描述】:

这个问题在这里被问过很多次,但没有一个解决方案对我有用,因为我的 XML 布局有点不同。

我有一个带有LinearLayout 的XML,其中包含一个ImageView 和一个TextView。 XML 是使用 Java 代码填充的。它是来自here 的文件选择器库,它列出了 android 文件系统中存在的文件和文件夹。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal" >

   <ImageView
        android:id="@+id/file_picker_image"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="5dip"
        android:contentDescription="@string/app_name"
        android:scaleType="centerCrop"
        android:src="@drawable/file" />

    <TextView
        android:id="@+id/file_picker_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:singleLine="true"
        android:text="@string/file_name"
        android:textSize="28sp" />

</LinearLayout>

现在,我想在此列表下方添加一个按钮。当我尝试在 XML 中添加按钮时,它会遍历每一行,并且按钮会显示在每一行中。 当我使用 Java 代码执行此操作时,按钮较近,也不显示 textview,但显示的是图像视图。

我想要的和this帖子中的图片类似,但直到现在都无法实现。我在此处阅读的这些解决方案或任何其他解决方案均不适用于我的情况。

我正在尝试在 Activity 中使用此代码添加按钮,其源代码在上面提供的链接中。我尝试在 FilePickerListAdapter 的构造函数中添加以下代码。

            RelativeLayout relLayout = new RelativeLayout(context);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

            Button filterBtn = new Button(context);
            filterBtn.setText("Filter");

            relLayout.addView(filterBtn, params);

请指导我做错了什么,以及如何使它成为可能。

问候。

【问题讨论】:

  • 发布您的列表视图 xml
  • 只是一个意见,你为什么不在RelativeLayout完成整个事情并使用android:layout_below=""

标签: android xml button layout


【解决方案1】:

混合使用 xml 方式和编程方式来创建视图并不是一个好主意。您不妨先在 xml 中创建所有视图(包括您的按钮),只需将您的按钮设置为 android:visibility="gone" 即可隐藏它。在需要时将其设置为可见。

【讨论】:

  • 我不确定在我的情况下是否可以这样做。我将编辑我的问题。
【解决方案2】:

这可能不是您要求的,但您可以尝试一下。我在您的 xml 中实现了layout_weight,并在屏幕末尾包含了按钮。其结果将与您问题中的链接类似。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight=".9" >

       <ImageView
           android:id="@+id/file_picker_image"
           android:layout_width="40dip"
           android:layout_height="40dip"
           android:layout_marginBottom="5dip"
           android:layout_marginLeft="5dip"
           android:layout_marginTop="5dip"
           android:contentDescription="@string/app_name"
           android:scaleType="centerCrop"
           android:src="@drawable/file" />

       <TextView
           android:id="@+id/file_picker_text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dip"
           android:singleLine="true"
           android:text="@string/file_name"
           android:textSize="28sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0.1" >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />  <!--Your Button-->

    </LinearLayout>

</LinearLayout>

【讨论】:

  • 可能是我以不同的方式发布了整个内容。让我再尝试一次。我无法在 XML 中添加 Button,因为它会在循环中自行迭代。甚至@IssacZH 的方式也将按钮保持在循环中。我将编辑我的问题。
  • 我明白了,我可能错过了你的那句话。所以这意味着您只想以编程方式在屏幕末尾添加按钮?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2020-10-22
  • 2014-04-26
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多