将按钮添加到包含ListView的XML文件中

时间:2018-07-18 16:55:50

标签: java android android-studio listview button

我一直在尝试在包含ListView的XML文件中实现三个按钮。但是,这些按钮在添加到集合时会被添加到各个视图中。有谁知道如何将它们分开以使按钮与ListView分开?

谢谢!

按钮XML的代码

<android.support.constraint.ConstraintLayout 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.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="85dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/ClearAll"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:onClick="onClickClearAll"
            android:text="Clear All Checks"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/checkAll"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/checkAll"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:onClick="onClickCheckAll"
            android:text="Check All Devices"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@+id/ClearAll"
            app:layout_constraintTop_toTopOf="parent" />


    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

ListView代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/device_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/device_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp" />

    <ListView
        android:id="@+id/lvCheckBox"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="@null"
        android:fadingEdge="none">
    </ListView>

    <CheckBox
        android:id="@+id/CheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
</LinearLayout>

组合XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/ClearAll"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_above="@id/checkAll"
        android:onClick="onClickClearAll"
        android:text="Uncheck All" />

    <Button
        android:id="@+id/checkAll"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClickCheckAll"
        android:text="Check All"/>

    <Button
        android:id="@+id/passCredentials"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClickPassCredentials"
        android:text="Enter Credentials" />

    <TextView
        android:id="@+id/device_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/device_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp" />

    <ListView
        android:id="@+id/lvCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_above="@id/ClearAll"
        android:layout_alignParentTop="true"
        android:layout_weight="1.0"
        android:cacheColorHint="@null"
        android:fadingEdge="none">
    </ListView>

    <CheckBox
        android:id="@+id/CheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
</LinearLayout>

0 个答案:

没有答案