我遇到了问题我无法解决,如果有人帮助我,我会很感激。我想在屏幕顶部放两张图片。两个图像都应该有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>
答案 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)
<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