Android ImageView宽度为设备宽度的50%

Android ImageView宽度为设备宽度的50%,android,imageview,android-cardview,Android,Imageview,Android Cardview,在开始之前,我应该说我是Android开发的初学者:) 我正在使用网格recyclerView,并已将列数设置为2。 我的cardwiew中只有一张ImageView,我不知道如何将这张卡(或图像)设置为屏幕宽度的50%,因为当我用数字设置其高度和宽度时,我会在不同的屏幕尺寸上遇到问题 我阅读并尝试了一些使用layout\u weight的方法,但它不适用于此cardwiew card.xml 我在这里怎么做 提前谢谢这样的事情在你年轻时就出现了。你可以阅读更多关于它的信息 如果不使用此库,

在开始之前,我应该说我是Android开发的初学者:) 我正在使用网格
recyclerView
,并已将列数设置为2。 我的
cardwiew
中只有一张
ImageView
,我不知道如何将这张卡(或图像)设置为屏幕宽度的50%,因为当我用数字设置其高度和宽度时,我会在不同的屏幕尺寸上遇到问题

我阅读并尝试了一些使用
layout\u weight
的方法,但它不适用于此
cardwiew

card.xml


我在这里怎么做


提前谢谢

这样的事情在你年轻时就出现了。你可以阅读更多关于它的信息

如果不使用此库,我只能建议您使用以下
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\u sum
,只需在子布局中使用
layout\u weight
并指定值即可。如果要分别垂直或水平设置分区,请确保将
高度
宽度
设置为0


PS.
layout\u weight
仅适用于线性布局。

通过使用百分比相对布局,我们可以轻松实现这一点。在此,我将解释如何实现
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>


希望这有帮助:)

cardwiew
扩展自
FrameLayout
。如果您确实想使用
CardView
,请在CardView中放置一个
LinearLayout
,然后您可以使用attr layout\u weight
<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>