如何缩放图像,使其始终是Android RelativeLayout中屏幕大小的75%?

时间:2014-04-01 20:25:15

标签: android image android-layout imageview relativelayout

我在RelativeLayout中有一个图像,我希望按比例放大和缩小,以便占用相同的空间百分比,无论用户具有什么屏幕尺寸。我希望图像大约是屏幕宽度的75%,无论是在平板电脑上还是在手机上观看应用程序。

我正在努力找出实现这一目标的最佳方法,因为似乎layout_weight在相对布局中不起作用。在这种情况下,获得百分比宽度的最佳方法是什么?

4 个答案:

答案 0 :(得分:0)

尝试左右边距播放

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

根据您的需要更改10dp

答案 1 :(得分: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>

现在享受吧。

答案 2 :(得分: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>

答案 3 :(得分: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);