2012-09-06 4 views
6

私はAndroidで非常に新しいので、おそらく愚かな質問かもしれませんが、ここには行きます。Android:画面解像度のパーセンテージでのレイアウトの高さ

私はアクティビティのRelativeLayoutを持っています。このレイアウトでは、画面の下部に別のレイアウトがあります。時には私はそれを隠すか見えるようにしますが、これは重要ではありません。別のレイアウトの高さを、画面全体の高さの27%とすることは可能ですか?アイデアは、画面上にこのレイアウトを除く他のコンテンツを保持することです。

誰もがこのようなことを試したことがありますか?

+0

は、あなたがあなたの画面の2つの部分をしたいと言うことを意味しますか?または、表示されているときに親全体の下位27%をカバーする別のビューの内側にあるビューを希望しますか? – brianestey

答えて

22

LinearLayoutandroid:layout_weightでこれを処理できます。例えば、ここではそれぞれ50%、30%、及び高さの20%を占めて三個のButtonウィジェットを含むレイアウトである:

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

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="50" 
     android:text="@string/fifty_percent"/> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="30" 
     android:text="@string/thirty_percent"/> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="20" 
     android:text="@string/twenty_percent"/> 

</LinearLayout> 

ただし、単に任意の時点でその任意のウィジェットを宣言することができませんあなたのレイアウトファイルでは、画面サイズの任意の割合を占める必要があります。あなたはJavaで計算を実行し、必要に応じてウィジェットのLayoutParamsを調整することもできます。

+0

パーフェクトソリューション –

1

これを試すことができますが、より複雑ですが便利です。トップは、画面の73%と第二されて27%であること -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/parent_view" 
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:text="@string/app_name" /> 

<LinearLayout 
    android:id="@+id/parent_of_bottom_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_alignParentBottom="true" > 

    <LinearLayout 
     android:id="@+id/content_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="27"> 

     //Add your content here 

    </LinearLayout> 
</LinearLayout> 

+0

この機能の仕組みや、なぜその質問に答えるのかについては、拡張することができますか? –

+0

あなたはRelativeLayoutの中でandroid:layout_weightを使うことはできませんので、私は相対レイアウトの中でweigntを比例関係にするためにLinearLayoutを使いました。私のコードプロジェクトから解決策を見つけました。 – wSakly

+0

ありがとう、それは問題への面白いアプローチです。 –