2016-05-02 113 views
1

使用XML作为根布局的相对布局,是否有办法让图像视图可以使用80%的屏幕并居中居中。显示图像占80%的屏幕

如果我使用包装内容或数字DP,那么图像可能在大屏幕上太小,或者在小屏幕上太大。无论屏幕是什么样子,我都希望它是80‰。

请让我知道你会如何处理这得益于

回答

1

您使用PercentRelativeLayout,导入此

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

在XML中,您使用<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

<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>