ImageView-根据Android屏幕调整大小

时间:2014-08-23 11:58:07

标签: java android android-layout

我遇到了问题我无法解决,如果有人帮助我,我会很感激。我想在屏幕顶部放两张图片。两个图像都应该有width = screen_width / 2(只是因为当屏幕较小时,图像应该调整大小)。我试过这个,但它还不够。谢谢!

 <RelativeLayout
    android:id="@+id/imagelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/r"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"    
    android:layout_marginLeft="3dp" 
        android:background="@drawable/back" />
    <TextView
        android:id="@+id/resulttext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:textSize="30sp"
        android:textStyle="bold"
        android:text="Hello"
        android:layout_alignLeft="@id/r"
        android:layout_alignTop="@id/r"
        android:layout_alignRight="@id/r"
        android:layout_alignBottom="@id/r"/>
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

使用LinearLayout代替/在RelativeLayout内部,并在每个子视图中将weight属性设置为0.5。你还应该使用layout:gravity来设置顶部的子视图。

答案 1 :(得分:0)

试试这个:

private int columnWidth;
//get Screen Size
public int getScreenWidth() {
            int columnWidth ;
            WindowManager wm = (WindowManager) this
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();

            final Point point = new Point();
            try {
                display.getSize(point);
            } catch (java.lang.NoSuchMethodError ignore) { // Older device
                point.x = display.getWidth();
                point.y = display.getHeight();
            }                              
            columnWidth = point.x;
            return columnWidth;
        }

 //set height and width of Imageview
int height=columnWidth;
int width=columnWidth;
img.setLayoutParams(new RelativeLayout.LayoutParams(height,width));

希望这可以帮到你

答案 2 :(得分:0)

供参考:http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CC0QtwIwAg&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DmlojNcvoW68&ei=RoX4U8zwLdbvaKmOgMgH&usg=AFQjCNE5rRlHfVw-n_jgZAXEhK-Q1dd_Mg&sig2=mt8454k4FzqoVBprIqorNg&bvm=bv.73612305,d.d2s

<RelativeLayout                                //Change this to a linear layout
android:id="@+id/imagelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" 

  android:weightSum="2" >                   //add this to linear layout attribute

<ImageView
    android:id="@+id/r"
    android:layout_width="0dp"          //change width to 0dp (in case of horzontal layout)
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"    
    android:layout_marginLeft="3dp" 
    android:background="@drawable/back" 

    android:layour_weight = "1"/>         //add this to both view

<TextView
    android:id="@+id/resulttext"
    android:layout_width="0dp"           //change width to 0dp (in case of horzontal layout)
    android:layout_height="wrap_content" 
    android:textSize="30sp"
    android:textStyle="bold"
    android:text="Hello"
    android:layout_alignLeft="@id/r"
    android:layout_alignTop="@id/r"
    android:layout_alignRight="@id/r"
    android:layout_alignBottom="@id/r"

    android:layour_weight = "1"/>         //add this to both view/>
</RelativeLayout>                             //This again has to be linear layout