布局问题 - 以编程方式添加按钮以进行查看

时间:2015-11-08 22:13:56

标签: android android-layout

我是Android Studio新手,我正在使用空白活动模板来尝试解决一些问题,特别是如何以编程方式将按钮添加到列表中的视图。

我有一个List,其中包含每个按钮的文本以及将在它们的点击事件中使用的其他一些数据。

首先,这是我正在使用的activity_main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <Button
            android:id="@+id/play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Play Sound"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textSize="48dp" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:id="@+id/menuLayout">

        </LinearLayout>



    </RelativeLayout>



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

在play_sound按钮的onClick事件中,我在列表上进行迭代,然后为需要在具有id menuLayout的LinearLayout中创建的按钮的每个项调用此方法:

private void createButton(String name){
    Button myButton = new Button(this);
    myButton.setText(name);


    LinearLayout ll = (LinearLayout)findViewById(R.id.menuLayout);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    ll.addView(myButton);
}

按钮是在Play Sound按钮后面创建的......我在Stack Exchange上搜索了很多类似的帖子并尝试了各种各样的东西,但我一直无法弄清楚如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您将menuLayout的高度设置为match_parentRelativeLayout表示它将填充其父级,无论其他视图如何。如果您希望它仅显示在play_button下方,则应添加layout_below参数,例如android:layout_below="@id/play_button

相关问题