簡體   English   中英

帶有ListView和按鈕的Android布局

[英]Android Layout with ListView and Buttons

好吧,這個特定的布局讓我煩惱。 並且似乎無法找到一種方法來使用listView,底部有一排按鈕,因此listview不會延伸到按鈕的頂部,因此按鈕總是被捕捉到屏幕的底部。 這就是我想要的:

刪除了死的ImageShack鏈接

看起來它應該很容易,但我嘗試過的一切都失敗了。 有幫助嗎?

這是我目前的代碼:

    RelativeLayout container = new RelativeLayout(this);
    container.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    //** Add LinearLayout with button(s)

    LinearLayout buttons = new LinearLayout(this);

    RelativeLayout.LayoutParams bottomNavParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    bottomNavParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    bottomNavParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttons.setLayoutParams(bottomNavParams);


    ImageButton newLayer = new ImageButton(this);
    newLayer.setImageResource(R.drawable.newlayer);
    newLayer.setLayoutParams(new LinearLayout.LayoutParams(45, LayoutParams.FILL_PARENT));
    buttons.addView(newLayer);

    container.addView(buttons);

    //** Add ListView

    layerview = new ListView(this);

    RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    listParams.addRule(RelativeLayout.ABOVE, buttons.getId());

    layerview.setLayoutParams(listParams);

    container.addView(layerview);

我想這就是你要找的東西。

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

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/testbutton"
        android:text="@string/hello" android:layout_alignParentBottom="true" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/list"
        android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />

</RelativeLayout>

我多年來一直有同樣的問題。

將ListView保持在按鈕上方但阻止它在列表很長時覆蓋它們的解決方案是在ListView上設置android:layout_weight =“1.0”。 將layout_weight保留在​​未設置的按鈕上,以使它們保持自然大小,否則按鈕將縮放。 這適用於LinearLayout。

Android ApiDemos中有一個例子: ApiDemos / res / layout / linear_layout_9.xml

我只是在尋找這個問題的答案,這是最初的結果之一。 我覺得好像所有的答案,包括目前被選為“最佳答案”的答案都沒有解決被問到的問題。 正在陳述的問題是ButtonListView這兩個組件有重疊,因為ListView占據了整個屏幕,而Button在視覺上漂浮在ListView的上方(前面)(阻塞視圖/訪問ListView的最后一項)

基於我在這里和其他論壇上看到的答案,我終於得出了如何解決這個問題的結論。

最初,我有:

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

  <ListView
    android:id="@+id/game_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    />

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    style="@android:style/ButtonBar">

    <Button
      android:id="@+id/new_game"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/new_game"
      android:textColor="#FFFFFFFF"
      android:background="@drawable/button_background" />
  </LinearLayout>

</RelativeLayout>

請注意使用RelativeLayout作為根節點。

這是Button不與ListView重疊的最終工作版本:

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

  <ListView
    android:id="@+id/game_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_weight="1.0" />

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    style="@android:style/ButtonBar">

    <Button
      android:id="@+id/new_game"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/new_game"
      android:textColor="#FFFFFFFF"
      android:background="@drawable/button_background" />
  </LinearLayout>

</LinearLayout>

只有兩個不同之處。 首先,我已經切換到使用LinearLayout 這將有助於下一位,即將android:layout_weight添加到我的ListView

我希望這有幫助。

最好的方法是設置列表視圖下方按鈕的相對布局。 在這個例子中,按鈕也是線性布局,因為它們更容易並排放置在相同的尺寸。

<RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<ListView android:id="@+id/ListView01" 
android:layout_alignParentTop="true"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
</ListView>

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_below="@+id/ListView01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true">
<Button android:id="@+id/ButtonJoin" 
android:text="Join"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true">
</Button>
<Button android:id="@+id/ButtonJoin" 
android:layout_alignRight="@id/ButtonCancel" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:layout_alignParentBottom="true">
</Button>
</LinearLayout>

</RelativeLayout>

我知道這篇文章相當陳舊,但是,為了回答原始海報的問題,代碼不起作用的原因是buttons.getId()返回-1。 如果你打算這樣做,你需要設置像調用buttons.setId(10)這樣的東西。 如果你這樣做,代碼工作得很好。

這應該工作。 在列表視圖上方也有按鈕,將按鈕放在另一個線性布局中。

<LinearLayout> main container // vertical

<LinearLayout> scrollview must be contained in a linear layout //vertical - height to fill parent

    <ScrollView> set the height of this to fill parent

        <ListView> will be contained in the scrollview
        </ListView>

    </ScrollView>

</LinearLayout>

<LinearLayout> //horizontal - height to wrap content

<Button>

</Button>

</LinearLayout>

</LinearLayout>

最簡單的解決方案是創建兩個線性布局,一個使用按鈕,另一個使用列表視圖(在按鈕高度上包裝內容並在列表布局高度上匹配父級)。 然后只使用列表視圖在布局上滾動視圖,按鈕布局將被忽略。 希望它有所幫助,抱歉我不想寫出代碼。

暫無
暫無

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

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