2014-04-01 59 views
0

我在RelativeLayout中有一个图像,我想要向上和向下缩放以占用相同的空间百分比,而不管用户具有什么屏幕大小。我希望图片的大小约为屏幕宽度的75%,无论该应用是在平板电脑上还是在移动电话上查看。如何缩放图像,使其始终为Android RelativeLayout中屏幕大小的75%?

我正在努力想出实现这一目标的最佳方法,因为layout_weight似乎不适用于相对布局。在这种情况下达到百分比宽度的最佳方式是什么?

+0

'似乎layout_weight不相对layouts'工作 - 它没有按”吨。它只适用于** LinearLayouts **。并且一次只能**一维**。 –

回答

0

尝试用保证金右打左

android:layout_width="fill_parent" 
android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp" 

变化10dp根据您的需要

0

我认为你是错的。 Layout_weight仍然是最好的方法。 只需在RelativeLayout中制作一个LinearLayout。然后让这个LinearLayout匹配父宽度并包装其高度的内容。现在将图像放入此布局后,在其中添加另一个像另一个LinearLayout。现在使用Layout_weight具有75%的屏幕宽度。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 
     </LinearLayout> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="6" 
      android:src="my_source"/> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 
     </LinearLayout> 

    </LinearLayout> 


</RelativeLayout> 

现在只要享受它。

+0

感谢您的帮助!有一点调整我有这种工作。它似乎适用于某些设备,但不适用于其他设备。此外,图像现在在我的页面上垂直居中,但我希望它坐在顶部。 – user1636130

0

我同意的LinearLayout是要走的路:

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

    <RelativeLayout 
     android:id="@+id/relative_layout_75_percent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".75" > 


    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/relative_layout_25_percent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".25" > 

    </RelativeLayout> 
</LinearLayout> 
0

你可以尝试这样的事情:

 DisplayMetrics metrics = getResources().getDisplayMetrics(); 
     ImageView mImageView = (ImageView) findViewById(R.id.mImageView); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels*0.75), RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     mImageView.setLayoutParams(params);