2016-05-02 47 views
1

相対レイアウトでXMLをルートレイアウトとして使用すると、画面の80%を使用して中央に配置するようにイメージビューに指示することができます。画面の80%を表示する画像を表示する

ラップコンテンツまたは数字のdpを使用すると、大きな画面では画像が小さすぎたり、小さな画面では大きすぎます。画面がどのようになっても80‰にしたいですか?

私はあなたが

答えて

1

を使用することができ、XMLではこの

dependencies { 
    ... 
    compile 'com.android.support:percent:23.3.0' 
    ... 
} 

をインポートし、あなたが<android.support.percent.PercentRelativeLayout>代わり<RelativeLayout>を使用し、それはそれらの子

app:layout_heightPercent="80%" 
app:layout_widthPercent="80%" 
ためのコードの下にサポートします

android:layout_width/heightを使用すると、あなたは "wrap_content"を使うことができます。

+0

恐ろしい次のように直線的なレイアウトに重みを使用して作成することができます。私はこれが存在することを知らなかった! – Snake

0

さて、あなたはこれらの数学的な操作が実行できない原因となるXMLにこれを行うことはできません。このおかげでアプローチする方法を教えてください。 XMLでMATCH_PARENTを使用できます。

次に、プログラムによって特定のViews MeasuredWidth()を取得し、その幅をその80%に設定します。

この方法ではるかに簡単で清潔です。

0

あなたがPercentRelativeLayoutを使用PercentRelativeLayout

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<ImageView 
    app:layout_widthPercent="80%" 
    app:layout_heightPercent="80%" 
    android:layout_centerInParent="true"/> 
</android.support.percent.PercentFrameLayout> 
0

あなたは

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="8" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="8"> 

       <ImageButton 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

    </LinearLayout> 
</RelativeLayout> 
関連する問題