ImageView 宽度为设备宽度的 50% Android
ImageView width to be 50 percent of Device width Android
在我开始之前我应该说我是 Android 开发的初学者 :)
我正在使用网格 recyclerView
并将列数设置为 2。
我的 cardView
中只有一个 ImageView
,我不知道如何使这张卡片(或图像)成为屏幕宽度的 50%,因为当我用数字设置它的高度和宽度时我在不同的屏幕尺寸上遇到问题。
我阅读并尝试了一些使用 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中出现了这样的事情。您可以阅读更多相关信息 here and 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
的 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>
希望这对您有所帮助:)
在我开始之前我应该说我是 Android 开发的初学者 :)
我正在使用网格 recyclerView
并将列数设置为 2。
我的 cardView
中只有一个 ImageView
,我不知道如何使这张卡片(或图像)成为屏幕宽度的 50%,因为当我用数字设置它的高度和宽度时我在不同的屏幕尺寸上遇到问题。
我阅读并尝试了一些使用 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中出现了这样的事情。您可以阅读更多相关信息 here and 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
的 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>
希望这对您有所帮助:)