如何在CardView中使图像居中

问题描述 投票:0回答:6

我无法在桌面中央获取图像。图像始终位于左侧。它是LinearLayout 我的一些ScrollView。

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardCornerRadius="80dp"
            app:cardElevation="10dp"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/titelbild280x280"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>

        </android.support.v7.widget.CardView>
android xml android-layout
6个回答
8
投票

CardView 是 FrameLayout 的扩展。因此,您应该使用与 FrameLayout 中相同的布局值。
要将子视图居中,请使用:

android:layout_gravity="center"

所以你的布局变成:

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="80dp"
        app:cardElevation="10dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/titelbild280x280"
            />

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

但是请注意,此示例中的 CardView 使用了wrapp_content,因此它将牢牢地包裹图像,并且不会留下任何内容居中。为了使重力起作用,CardView 必须大于 ImageView。


2
投票

您必须使用布局(

ConstraintLayout
RelativeLayout
LinearLayout
)来管理卡片视图中的多个视图 目前android UI系统与
ConstraintLayout
深度耦合,可以轻松处理复杂的布局,因此以下代码可能对您有帮助

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="80dp"
    app:cardElevation="10dp"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/titelbild280x280"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

    </android.support.constraint.ConstraintLayout>

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

谢谢。


0
投票

试试这个希望它对你有用

 <android.support.v7.widget.CardView
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="80dp"
        app:cardElevation="10dp"
        android:gravity="center"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/featureimage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:src="@drawable/image"/>


    </RelativeLayout>


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

0
投票

只需尝试一下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/some_img" />

</androidx.cardview.widget.CardView>

0
投票

请尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:gravity="center">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp"
        android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/titelbild280x280"
                android:layout_gravity="center"/>
    </android.support.v7.widget.CardView>

</LinearLayout>

我希望它对你有用。


0
投票

对我来说最简单的方法是在我的cardView中使用相对布局,然后使用android:layout_centerInParent =“true”。 当相对布局的宽度和高度与父级匹配(父级是cardView)时,这非常有效。

© www.soinside.com 2019 - 2024. All rights reserved.