ImageView宽度为设备宽度的50%Android
问题描述:
开始之前我应该说我是Android开发的初学者:) 我正在使用网格recyclerView
并已将列数设置为2 我仅限于在我的cardView
中有一个ImageView
,我不知道如何使此卡(或图像)为屏幕宽度的50%,因为当我使用数字设置其高度和宽度时,我在不同的屏幕尺寸上遇到问题。ImageView宽度为设备宽度的50%Android
我阅读并尝试了一些与layout_weight
的方法,但它不适用于此cardView
。
card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="200dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_photo_double_view"
android:src="@drawable/e48"
/>
</LinearLayout>
我怎么能做到这一点吗?
在此先感谢
答
这样的事情在ConstraintLayout已经appered。你可以阅读更多关于它here和here。
不使用这个库我只能用你喜欢LinearLayout
这个建议:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
正如你所看到的。如果linearlayout占据整个屏幕,我使用LinearLayout
的android:weight
属性使ImageView占用屏幕宽度的50%。
LinearLayout中没有必要定义weight_sum
,只需在子布局中使用layout_weight
并赋值即可。如果要分别垂直或水平分区,请务必将height
或width
设置为0。
PS。 layout_weight
仅适用于LinearLayouts。
答
通过使用百分比相对布局,我们可以轻松实现。这里我将解释如何实施percentRelativeLayout
。
相关性:
compile 'com.android.support:percent:23.3.0'
在你的布局文件,
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
app:layout_widthPercent="match_parent"
app:layout_heightPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
希望这是有益:)
'CardView'从'FrameLayout'延伸。如果你真的想使用'CardView',在CardView中放置一个'LinearLayout',那么你可以使用attr layout_weight –