[英]Listview and a button in the bottom of the screen
我已經嘗試了一切,然后到處抬頭。
我試圖將列表視圖放在滾動視圖中,並固定了屏幕底部的按鈕。這是我的代碼:
<?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"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rlCommento" />
<RelativeLayout
android:id="@+id/rlCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<EditText
android:id="@+id/etCCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="15"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bCInviaCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/etCCommento"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight="85"
android:text="Commenta!" />
</RelativeLayout>
</RelativeLayout>
在項目中似乎可以正常使用,但是當我在模擬器上嘗試時,它不起作用! 有什么建議嗎? 謝謝!
這是您要做的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
您可以使用LinearLayout。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RelativeLayout
android:id="@+id/rlCommento"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
</LinearLayout>
首先:不要將列表視圖放在滾動視圖中;其次:使您的最上層父級寬度n高度與父級相匹配第三:在底部添加新布局,並在此布局內添加按鈕,類似這樣,為布局賦予一些權重
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingBottom="7dp"
android:paddingTop="7dp" >
<Button
android:id="@+id/score_board"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="center"
android:layout_weight="1"
android:text="OK" />
</LinearLayout>
// Try this way,hope this will help you to solve your problem...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="@+id/rlCommento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<EditText
android:id="@+id/etCCommento"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bCInviaCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Commenta!" />
</LinearLayout>
</LinearLayout>
為此,您必須垂直於LinearLayout而不是RelativeLayout並設置ListView weight = 1和layout_height = 0dp。 這應該根據需要工作:
<?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" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RelativeLayout
android:id="@+id/rlCommento"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/etCCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="15"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bCInviaCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/etCCommento"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight="85"
android:text="Commenta!" />
</RelativeLayout>
希望我能正確理解您的問題。 無需將ListView保留在scrollview中。 只需嘗試按照以下代碼段即可。 它將為您服務。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/bCInviaCommento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Commenta!"
android:layout_below="@id/list" />
</RelativeLayout?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.