Android图像背景高度

时间:2014-07-21 12:33:04

标签: android

我写这段代码:

android:background="@drawable/back"

此代码加载图像并设置全屏模式
但我想设置为屏幕高度的70%左右我该怎么办?

2 个答案:

答案 0 :(得分:0)

public static int width = 0, height = 0;

/* GET WIDTH AND HEIGHT OF SCREEN */
if (width == 0 && height == 0) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        w.getDefaultDisplay().getSize(size);
        width = size.x;
        height = size.y;
    } else {
        Display d = w.getDefaultDisplay();
        width = d.getWidth();
        height = d.getHeight();
    }
}

这里你可以像这样设置设备高度和宽度70%

RelativeLayout.LayoutParams mPar = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (height*0.70));

答案 1 :(得分:0)

我的这对你有帮助:

将ImageView的android:layout_height="0dp"android:layout_weight="0.70",其他不可见视图设为30%。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutexample"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.70"
        android:src="@drawable/ic_launcher" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.30"
        android:visibility="invisible" />

</LinearLayout>