【问题标题】:how to center crop a background image of linear layout?如何居中裁剪线性布局的背景图像?
【发布时间】:2018-04-28 07:17:29
【问题描述】:

您好,我必须使用带有 recyclerview 的卡片进行布局,因此我将图像设置为线性布局的背景,但现在我无法居中裁剪该图像 我的问题是我不能使用 imageview,因为使用它的卡片高度也会增加,我不希望这样,所以请如果有人可以帮助我.. 我的xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/primary_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="19dp"
    android:layout_marginRight="19dp"
    android:layout_marginTop="15dp"
    app:cardCornerRadius="115dp">

    <ImageView
        android:id="@+id/background_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@drawable/club1"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/ellipse" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/first_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="John Doe"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/second_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="checked in to"
                        android:textColor="@color/White"
                        android:textSize="10sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/third_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="W south"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:orientation="horizontal">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fourth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="9dp"
                        android:text="beach mumbai"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fifth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/second_text"
                        android:layout_toRightOf="@+id/fourth_text"
                        android:text="30 mins ago."
                        android:textColor="@color/White"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="85dp">

            <com.ct.listrtrial.Custom.CustomTextViewMedium
                android:id="@+id/sixth_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:text="reply to abc............"
                android:textColor="@color/White" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/favourite_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_favorite_border_black_24dp" />

                <com.ct.listrtrial.Custom.CustomTextViewMedium
                    android:id="@+id/seventh_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:text="40 likes"
                    android:textColor="@color/White" />
            </LinearLayout>
        </RelativeLayout>

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

【问题讨论】:

  • 对线性布局使用边距
  • 但我想将线性布局的裁剪图像居中
  • 使用另一个线性布局作为父级并为此使用填充。对于背景,您不能使用这些图像属性。
  • 但即使我使用另一种无法解决裁剪问题的线性布局进行填充
  • 你能发布你所期望的截图吗?

标签: java android user-interface


【解决方案1】:

您不能居中裁剪线性布局的背景图片, 但是您可以使用这些更改来实现相同的效果:

  1. 将 LinearLayout 替换为 RelativeLayout
  2. 将 ImageView 作为第一个子项放置在相对布局中,并使用属性进行中心裁剪。

    <RelativeLayout
    android:id="@+id/background_image"
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:orientation="vertical">
    
        <ImageView
        android:scaleType="centerCrop"
        android:src="@drawable/club1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    .....
    ......
    </RelativeLayout>
    

【讨论】:

    【解决方案2】:

    如果你想控制 Image View Scale

    你需要把它放在 ImageView As Src Not Background

    你可以使用Frame布局来实现它

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/primary_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="19dp"
        android:layout_marginRight="19dp"
        android:layout_marginTop="15dp"
        app:cardCornerRadius="115dp">
    
        <ImageView
            android:id="@+id/background_image_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:visibility="gone" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="210dp">
            <ImageView
                android:layout_width="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_height="match_parent" />
            <LinearLayout
                android:id="@+id/background_image"
                android:layout_width="match_parent"
                android:layout_height="210dp"
                android:orientation="vertical"
                >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <ImageView
                        android:id="@+id/profile_image"
                        android:layout_width="56dp"
                        android:layout_height="56dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:src="@drawable/playstore_icon" />
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
    
    
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:padding="5dp">
    
                            <TextView
                                android:id="@+id/first_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="18dp"
                                android:padding="5dp"
                                android:text="John Doe"
                                android:textSize="15sp" />
    
                            <TextView
                                android:id="@+id/second_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="18dp"
                                android:padding="5dp"
                                android:text="checked in to"
                                android:textSize="10sp" />
    
                            <TextView
                                android:id="@+id/third_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="18dp"
                                android:padding="5dp"
                                android:text="W south"
                                android:textSize="15sp" />
    
                        </LinearLayout>
    
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:orientation="horizontal">
    
                            <TextView
                                android:id="@+id/fourth_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="9dp"
                                android:text="beach mumbai"
                                android:textSize="15sp" />
    
                            <TextView
                                android:id="@+id/fifth_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/second_text"
                                android:layout_toRightOf="@+id/fourth_text"
                                android:text="30 mins ago."
                                android:textSize="10sp" />
                        </LinearLayout>
                    </LinearLayout>
    
    
                </LinearLayout>
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="85dp">
    
                    <TextView
                        android:id="@+id/sixth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerInParent="true"
                        android:padding="10dp"
                        android:text="reply to abc............"
                        android:textColor="@color/white" />
    
    
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerInParent="true">
    
                        <ImageView
                            android:id="@+id/favourite_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="5dp"
                            android:adjustViewBounds="true"
                            android:scaleType="fitXY"
                            android:src="@drawable/ic_close" />
    
                        <TextView
                            android:id="@+id/seventh_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="15dp"
                            android:text="40 likes"
                            android:textColor="@color/white" />
                    </LinearLayout>
                </RelativeLayout>
    
            </LinearLayout>
    
        </FrameLayout>
    </android.support.v7.widget.CardView>
    

    【讨论】:

      【解决方案3】:

      CenterCrop 逻辑在 ImageView 中烘焙。正确的解决方案是将其提取到 a Drawable 中,该 a Drawable 包装了另一个 Drawable 并 centerCrops 。

      【讨论】:

        【解决方案4】:

        如果您使用 CoordinatorLayout,那么只需将带有 centerCrop 参数的 ImageView 放在顶部。

        <CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <ImageView
            android:scaleType="centerCrop"
            android:background="@drawable/background_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        
        <AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="0dp">
        
            <include layout="@layout/header_main" />
        
            <TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorColor="@android:color/white"
                android:background="@drawable/white_border_background"
                app:tabIndicatorHeight="3dp"/>
        
        </AppBarLayout>
        
        <ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </CoordinatorLayout>
        

        【讨论】:

          猜你喜欢
          • 2021-07-05
          • 2015-10-22
          • 2014-07-06
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多