簡體   English   中英

如何使用TOP上的TextView和BOTTOM上的ListView構建UI?

[英]How to build a UI with a TextView on TOP and a ListView on BOTTOM?

我在Internet上檢查了一些示例,這些示例僅顯示了如何單用ListView ,這很無聊。

我們總是需要構建一個復雜的UI,現在,我想編寫一個UI,其中有兩個View ,其中一個是TOP上的TextView ,並占用30%的空間,另一個是ListView ,它占用了其余的占70%。

如果我只是這樣寫:

<LinearLayout>
<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"/>
<ListView
    ....
    android:layout_weight="7"/>
</LinearLayout>

它不起作用....

頂部是Textview,下面是listview。 相應地修改以下內容。

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

   <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="40dp" // specify the height required for textview
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="TextView" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

您也可以將視圖添加為listview的標題。

嘗試這個 :

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

    </ListView>

</LinearLayout>

使用類似的相對布局:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="Large Text" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/textView1" >
</ListView>

</RelativeLayout>
<LinearLayout>
    <TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".3"/>
    <ListView
    ....
    android:layout_weight=".7"/>
</LinearLayout>

暫無
暫無

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

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