Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何创建屏幕宽度为4/5的布局_Android_Xml_Layout - Fatal编程技术网

Android 如何创建屏幕宽度为4/5的布局

Android 如何创建屏幕宽度为4/5的布局,android,xml,layout,Android,Xml,Layout,我正在实现一个水平回收视图,其中趋势类别卡片视图类似于此图 但我通过固定宽度来实现这一点。对于小屏幕cardview覆盖整个宽度,我不希望我的代码是 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-au

我正在实现一个水平回收视图,其中趋势类别卡片视图类似于此图

但我通过固定宽度来实现这一点。对于小屏幕cardview覆盖整个宽度,我不希望我的代码是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
    android:layout_width="300dp"
    android:layout_height="150dp"
    card_view:cardCornerRadius="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    card_view:cardElevation="1dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:id="@+id/trendimage"
            android:src="@drawable/placeholder" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:id="@+id/trendtitle"
            android:layout_gravity="bottom"
            android:hint="Zakir Khan's Event"
            android:textSize="14sp"
            android:background="#88000000"
            android:textColor="@color/icons" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:src="@drawable/place_white_24dp"/>
    </FrameLayout>

</android.support.v7.widget.CardView>
</LinearLayout>

您可以通过使用来实现这一点

第一步:
将百分比支持库添加到模块梯度文件中

compile 'com.android.support:percent:23.0.0'
第二步:
将百分比宽度添加到布局中:

示例:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        app:layout_widthPercent="80%"
        android:layout_height="150dp"
        card_view:cardCornerRadius="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        card_view:cardElevation="1dp">

        <android.support.percent.PercentRelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_dark">

            <ImageView
                android:id="@+id/trendimage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/test"/>

            <TextView
                android:id="@+id/trendtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="#88000000"
                android:hint="Test Event"
                android:padding="4dp"
                android:textSize="14sp"/>

            <ImageView
                app:layout_widthPercent="10%"
                app:layout_heightPercent="10%"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:src="@drawable/test"/>

        </android.support.percent.PercentRelativeLayout>

    </android.support.v7.widget.CardView>
    </android.support.percent.PercentRelativeLayout>


我希望这有助于

您可以计算屏幕的宽度,并将其中的4/5设置为适配器的onBindViewHolder调用。您可以在此处设置项目视图的布局参数(宽度)。

也许
值-w320dp
会有所帮助;)您需要根据亲子关系戏剧性地计算高度和宽度。@Chetan没有任何xml方法可以做到这一点?那么您应该使用权重或新的constraintView。您能提供相关代码吗。。约束布局是新的,我尝试过,但无法实现顶部布局为匹配父级,这将创建额外的间隙??父线性布局中的“匹配父级”属性将把视图的整个长度分配给线性布局。使用父布局中提供的weightSum属性,它指定可以根据父LinearLayout中提供的weightSum分布子视图,当我们将layout_weight属性指定为4时,它会根据您的要求分配精确的宽度,即“4/5”,请参见上图。。。它的回收器可以查看卡片,所以如果我定义匹配的父宽度。。然后第二张卡片将被放置在一些空格后。你说的是带有“趋势”标题的recyclerView和Arijit Singh的图片,对吗?好的,那么你想在没有任何额外空格的情况下附加视图?