代码之家  ›  专栏  ›  技术社区  ›  Abed Naseri

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

  •  1
  • Abed Naseri  · 技术社区  · 7 年前

    在我开始之前,我应该说我是Android开发的初学者:) 我正在使用网格 recyclerView 我只有一个 ImageView 在我的 cardView 我不知道如何使这张卡片(或图像)达到屏幕宽度的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>
    

    我在这里怎么做?

    2 回复  |  直到 7 年前
        1
  •  2
  •   deejay user370305    7 年前

    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>
    

    正如你所见。我使用 android:weight 的属性 线性布局

    无需定义 weight_sum layout_weight 在子布局中,并指定值。确保设置 height width 如果要分别在垂直或水平方向上进行分区,则返回0。

    附言 仅适用于线性布局。

        2
  •  1
  •   Jeevanandhan    7 年前

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

    希望这有帮助:)