簡體   English   中英

如何在Android中具有match_parent作為寬度和高度的列表視圖下面有一個按鈕

[英]How to have a button below a listview which has match_parent as width and height in android

我正在一個具有自定義listview的項目上工作。由於baseadapter的getView()方法一直被稱為listview中的行數的三次,有人建議我將listview的寬度和高度更改為match_parent 。但是現在問題是列表視圖下面有一個按鈕,現在不可見。如何使它可見。

我指的是: 自定義listview適配器的getView方法被多次調用,並且沒有一致的順序

我的xml文件:

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="LIST OF TASKS" />

<ListView
    android:id="@+id/list_tasks_final"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="50px"
     >

</ListView>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/list_tasks_final"
    android:layout_centerHorizontal="true"
    android:text="SUBMIT JOB"
    android:id="@+id/btnjobsubmit"
     />

</RelativeLayout>

問題在於該按鈕現在不可見,如何使其可見?

看到我的代碼,您應該將列表視圖包裝在另一個視圖LinearLayout ,然后將高度設置為該布局。

<?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="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LIST OF TASKS" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <ListView
            android:id="@+id/list_tasks_final"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="50px"
            >
        </ListView>
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="SUBMIT JOB"
        android:id="@+id/btnjobsubmit"
        />

</LinearLayout>

不應使用RelativeLayout ,而應使用LinearLayout 它可以根據我們的需求提供更大的靈活性來放置不同的視圖。

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:text="LIST OF TASKS" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ListView
            android:id="@+id/list_tasks_final"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"/>

    </LinearLayout> 

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="SUBMIT JOB"
    android:id="@+id/btnjobsubmit" />

</LinearLayout>

這里的問題是您的列表使用了屏幕的所有空間(Layout_height = match_parent,layout_width = match_parent),並且按鈕具有layout_below屬性,然后它顯示在整個屏幕下方。

要解決此問題,只需刪除以下layout_below屬性:

android:layout_below="@+id/list_tasks_final"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM