两个按钮显示仅在XML中创建一个

时间:2015-03-01 02:03:34

标签: android xml

我完全用XML创建我的布局。我希望显示ListView,并且屏幕底部始终有一个按钮,就像默认的FullScreenActivity一样。但是,问题是位于底部的按钮也出现在屏幕顶部。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
tools:context=".ChannelNameActivity">

<ListView
    android:id="@+id/channelListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:layout_weight="1"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/black_overlay"
    android:orientation="horizontal"
    tools:ignore="UselessParent">

    <Button
        android:id="@+id/dummy_button"
        style="?metaButtonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/dummy_button" />

</LinearLayout>

</RelativeLayout>

Duplicate button image

2 个答案:

答案 0 :(得分:0)

您必须在ListView中使用android:layout_above="@+id/lnlLayout"属性,在LinearLayout上使用android:alignParentBottom="true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
tools:context=".ChannelNameActivity">

<ListView
    android:id="@+id/channelListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:layout_above="@+id/lnlLayout"
    android:keepScreenOn="true"/>

<LinearLayout
    android:id="@+id/lnlLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/black_overlay"
android:layout_alignParentBottom="true" 
    android:orientation="horizontal"
    tools:ignore="UselessParent">

    <Button
        android:id="@+id/dummy_button"
        style="?metaButtonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/dummy_button" />

</LinearLayout>
</RelativeLayout>

答案 1 :(得分:0)

(OP上一个问题的代码说明:ArrayAdapter unable to update contents

因为在onCreate()中您将活动布局作为标题视图添加到ListView,这是不必要的(不是必需的)。

所以只需从onCreate()

中删除以下代码行即可
final View contentView = (View)getLayoutInflater().inflate(R.layout.activity_channelname, null);
channelListView.addHeaderView(contentView);