17

幅が線形レイアウトの次の3つのボタンがありますfill_parent

これらのボタンの幅を設定して、画面領域全体を均等にカバーするにはどうすればよいですか?

<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnReplyMessage" 
   android:gravity="left"
   android:text="Reply" 
/>
    
<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnMarkAsUnread"
   android:gravity="left" 
   android:text="Mark as unread" 
/>
            
<ImageButton 
   android:id="@+id/btnDeleteMessage"
   android:src="@drawable/imgsearch"
   android:gravity="right" 
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_alignParentRight="true"
 />
4

3 に答える 3

35

すべてのボタンに次のプロパティを指定します

android:layout_width="fill_parent"
android:layout_weight="1"

fill_parentできるだけ多くの幅を消費するように指示し、weight複数のコントロールが同じスペースで競合している場合に、その幅をどのように配分するかを決定します。(ボタンごとにの値を変えてみて、weightそれがどのように機能するかを確認してください)

于 2010-03-01T09:14:52.243 に答える
7

ボタンごとに次の属性を指定する必要があります。

android:layout_width="fill_parent"
android:layout_weight="1"

したがって、次のようになります。

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>
于 2010-03-01T09:14:49.747 に答える
2

次のコードを使用できます。

  <Button
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"/>

すべてのボタンで幅0dpを実行する必要があります。

于 2012-12-17T13:54:43.843 に答える