簡體   English   中英

Android的重量和布局寬度

[英]Android weight and layout width

我有一個線性布局和一個按鈕。 我希望按鈕適合布局寬度的90%。 在完成一些教程之后,我使用了layout_weight屬性,這就是我的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:background="#FFFFFF"
   android:weightSum="5" >

<Button 
   android:id="@+id/newgame"

   android:layout_width="0dp" 
   android:layout_height="wrap_content" 
   android:layout_weight="4"

   android:text="@string/newgame"
   android:textSize="16sp"
   android:typeface="monospace"
   android:textStyle="bold"
   android:background="@drawable/backrepeat" />

</LinearLayout>

現在,如果像大多數教程所說的那樣將layout_width設置為0dp,我的按鈕就會消失...可能是什么問題? 提前致謝。

線性布局權重僅在布局方向的維度上分配空間。 您具有垂直的線性布局,並且元素的寬度為0px-權重機制不會對此添加任何內容。 將線性布局方向更改為水平。

垂直方向!

垂直

您可以像這樣簡單地設置權重。... 必須使用0dip而不是0dp

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:weightSum="5" >

<Button
    android:id="@+id/newgame"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="4"
    android:background="@drawable/backrepeat"
    android:text="@string/newgame"
    android:textSize="16sp"
    android:textStyle="bold"
    android:typeface="monospace" />

</LinearLayout>

嘗試這個,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFFFF"
 >

 <Button 
    android:id="@+id/newgame"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="9"

    android:text="@string/newgame"
    android:textSize="16sp"
    android:typeface="monospace"
    android:textStyle="bold"
    android:background="@drawable/backrepeat" />

 </LinearLayout>

默認情況下,線性布局的方向是水平的,因此現在考慮水平寬度將其對齊。 :)

暫無
暫無

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

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