Android,如何在relativelayout中使用百分比设置imageview
我想建立一个这样的用户界面。 这是我的xml代码, 对于我的设计,我希望“效果”和“相机”结合为一个Android,如何在relativelayout中使用百分比设置imageview,android,image,user-interface,Android,Image,User Interface,我想建立一个这样的用户界面。 这是我的xml代码, 对于我的设计,我希望“效果”和“相机”结合为一个ImageView像链接中的“商店”一样 因此总共有5个ImageViews,我将id设置为链接中的名称 问题是,如何使用百分比设置高度和宽度 效果+camrea:高度25%,宽度100% <RelativeLayout android:id="@+id/mainContent" android:layout_width="fill_parent" android:layout_height
ImageView
像链接中的“商店”一样
因此总共有5个ImageView
s,我将id设置为链接中的名称
问题是,如何使用百分比设置高度和宽度
效果+camrea:高度25%,宽度100%
<RelativeLayout android:id="@+id/mainContent" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:background="#ffffff">
<ImageView
android:id="@+id/img_effect+camera"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/a" />
<ImageView
android:id="@+id/img_collage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img_effect+camera"
android:src="@drawable/b" />
<ImageView
android:id="@+id/img_draw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/img_effect+camera"
android:layout_toRightOf="@+id/img_collage"
android:src="@drawable/c" />
<ImageView
android:id="@+id/img_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/img_collage"
android:layout_below="@+id/img_draw"
android:src="@drawable/d" />
<ImageView
android:id="@+id/img_shop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img_photo"
android:src="@drawable/e" />
</RelativeLayout>
拼贴:高25%,宽50%
绘制:高度25%,宽度50%
照片:高50%,宽50%
店铺:高25%,宽100%
<RelativeLayout android:id="@+id/mainContent" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:background="#ffffff">
<ImageView
android:id="@+id/img_effect+camera"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/a" />
<ImageView
android:id="@+id/img_collage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img_effect+camera"
android:src="@drawable/b" />
<ImageView
android:id="@+id/img_draw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/img_effect+camera"
android:layout_toRightOf="@+id/img_collage"
android:src="@drawable/c" />
<ImageView
android:id="@+id/img_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/img_collage"
android:layout_below="@+id/img_draw"
android:src="@drawable/d" />
<ImageView
android:id="@+id/img_shop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img_photo"
android:src="@drawable/e" />
</RelativeLayout>
<代码> > p>您可以考虑在布局中使用Android:LayOutLoad Palm PARAM
在Xml中很难实现,您可以通过代码进行一些计算来实现。
如何做:
- 获取主内容高度和宽度,这是您的父视图
- 获取每个imageVIew的LayoutParams,并通过计算高度和宽度百分比设置高度和宽度
示例代码:
(活动中)
您必须使用布局权重,请检查您的问题。总高度高达125%。拼贴、绘画和拍照会在同一排吗?总的百分比是150%。这不是125%和150%,因为它们是按特殊顺序排列的,在这个链接中显示我更改了我的示例代码。让我知道它是否有效。如果它确实对您有帮助,请将我的答案标记为正确。谢谢:)它很有效!!我可以再问一个问题吗?例如,我的imageview是10X10,但我的图像是10X20,android设备会将图像显示为5X10,以确保图像显示良好。如何将其设置为原始图像大小,而忽略过多的部分?可能有点复杂,希望你能理解:)你应该看看ImageView中的android:scaleType参数-。使用所有可用选项,选择最适合您的选项。您总是有另一个选项:在传递到ImageView之前裁剪图像。请注意:如果将此代码放入onCreate方法中,parentView.getHeight()和.getWidth()将返回0,因为视图尚未绘制。应将其添加到何处?最好在View.post(Runnable)
方法中调用,这意味着在屏幕上绘制视图时将调用runnable
。以上eg已更新。
final View parentView= findViewById(R.id.mainContent);
final ImageView effect_camera= (ImageView)findViewById(R.id.img_effect+camera);
effect_camera.post(new Runnable(){
@Override
public void run(){
int height=parentView.getHeight();
int width=parentView.getWidth();
RelativeLayout.LayoutParams lp= (RelativeLayout.LayoutParams)effect_camera.getLayoutParams();
int percentHeight=height*.25;
int percentWidth= width*1;
lp.height=percentHeight;
lp.width=percentWidth;
effect_camera.setLayoutParams(lp);
}
});