以编程方式将Button添加到XML Layout

时间:2013-05-09 07:56:50

标签: android xml button layout

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

我有一个包含ImageView和TextView的LinearLayout XML。使用Java代码填充XML。它是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代码执行此操作时,按钮更近,也不显示文本视图,但会显示图像视图。

我想要的是与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);

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

问候。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这可能不是您要求的,但它是您可以尝试的替代方案。我在您的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>