2011-10-23 22 views
1

私の質問はとても簡単です。私はレイアウトを持っています:要素の幅をパーセントで設定するにはどうすればよいですか?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@drawable/mobile_background" 
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:layout_height="fill_parent" 
    > 
    <Button 
     android:text="GET MESSAGES" 
     android: 
     android:id="@+id/buttonMessages" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:text="REGISTRATION" 
     android:id="@+id/buttonRegistration" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:text="SETTINGS" 
     android:id="@+id/buttonSettings" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

しかし、私はボタンの固定サイズを設定する必要があります - 私は画面の幅の30%のサイズを設定する必要があります。どうしたらいいですか?

答えて

1

横のLinearLayoutsでボタンを入れ子にして、別のビューを横にしてパディングしてみましょう。ボタンを割り当て、android:layout_width="0dp"を表示し、ボタンandroid:layout_weight="3"とビューandroid:layout_weight="7"を入力します。これにより、ボタンが30%、空のビューが70%になります。それは30-70を設定するのは難しい三つのボタンで

<?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:gravity="center" 
    android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/buttonMessages" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="GET MESSAGES" /> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="7" /> 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/buttonRegistration" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="REGISTRATION" /> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="7" /> 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/buttonSettings" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="SETTINGS" /> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="7" /> 
    </LinearLayout> 

</LinearLayout> 
+0

他に意味がありますか? – user975290

+0

あなたは 'findViewById'を使ってアクティビティに動的に幅を設定することができますが、xmlのアプローチは頭痛の程度が低いと思います。 – Craigy

0

:)私はあなたがそれを得るでしょう、2でそれを行う方法を紹介します。

widthにfill_parentを使用すると、スペースは逆比例で分散されます。したがって、小さなボタンの場合は7、大きい場合は3を設定する必要があります。 1/x + 1/y + 1/z = 1とすると、30%、20%、50%の場合、比率の式が得られます。 1/X/3 = 1/Y/2 = 1/Z/5それとも、ただこれはXMLで行うことができない良い:)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@drawable/mobile_background" 
    android:layout_width="fill_parent" 
    android:gravity="center"/> 
    <Button 
     android:text="Narrow button" 
     android: 
     android:id="@+id/buttonMessages" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="7"/> 
    <Button 
     android:text="Wide button" 
     android:id="@+id/buttonRegistration" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="3"/> 
</LinearLayout> 
0

を見て値を試してみて、設定することができます。コードのサイズを指定する必要があります。

obj.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,0.25)); 

これは「親の身長の100%が、親の幅の25%を使用する」を意味:私は活動の中のonCreate()ことを行うだろう。

source

関連する問題