[英]Setting up a layout in an Android application, have some questions about locating the buttons
如標題所示,我正在使用Layout View和兩行按鈕來完成各種任務的Android應用程序。 一排按鈕位於頂部,一排按鈕位於底部,我喜歡這種外觀。
現在,對於不幸的部分:我使用了一對工具,您不應該再使用它們來完成此任務:AbsoluteLayout,按鈕位置(和大小)以px為單位,而不是dp或sp。
問題是,即使我使用了更高級的布局工具,我也不知道如何將按鈕組放置在屏幕底部。 那只是一個屏幕尺寸,遠小於第二個屏幕尺寸(這就是我現在得到的...)
現在,這永遠不會是一個公開可用的應用程序,因此我不必擔心通用兼容性,但是沒有人建議對頂部和底部的5個按鈕的一行進行簡單的布局XML描述嗎?屏幕的位置(在橫向模式下固定)???
另外,有人知道是否可以在運行時定位布局小部件嗎? 據我所知,很容易調整它們的大小 ,但不更改它們的位置?
謝謝,
R.
您的問題確實沒有很好的簡短答案。 最好的選擇是閱讀一個講解並向您展示發生了什么的教程。 我認為這對我來說是一個很好的起點。
您可以通過幾種方法來完成您所談論的內容,但我可能會使用RelativeLayout 。 Romain Guy的博客很好地解釋了如何使用它們。 簡而言之,一組按鈕將各自設置layout_alignParentTop="true"
,而其他按鈕會將layout_alignParentBottom
設置為layout_alignParentBottom
問題已解決。 您還可以在按鈕上設置android:id
,以便可以使用android:layout_toLeftOf
/ android:layout_toRightOf
將它們定位在彼此的左側/右側,或者可以使用android:orientation="horizontal"
每組按鈕粘貼在LinearLayout
android:orientation="horizontal"
。
至於在運行時重新定位元素,您絕對可以做到這一點-但您必須通過元素的LayoutParams
來做到這一點(或者創建適當類型的新LayoutParams
對象並將其分配給View
,或者通過調用getLayoutParams()
並修改返回的對象)。 對於RelativeLayout的子級,請參閱RelativeLayout.LayoutParams
。
是的,使用FrameLayout和layout_gravity很容易做到。
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
...
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
...
</LinearLayout>
</FrameLayout>
將視圖夾在兩個按鈕欄之間可能需要將FrameLayout更改為LinearLayout並將中心視圖上的layout_weight設置為1,而不指定按鈕欄的權重。
<?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"
>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top">
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_weight="1"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="bottom" android:layout_gravity="bottom" android:layout_weight="1">
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="bottom"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.