【问题标题】:How to not crop ImageView inside a LinearLayout?如何不在 LinearLayout 中裁剪 ImageView?
【发布时间】:2021-12-25 15:39:56
【问题描述】:

嗨,我正在尝试制作这样的东西

这就是我得到的

这是xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:layout_marginHorizontal="10dp">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/category_item_background"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:paddingBottom="20dp">

        <ImageView
                android:id="@+id/planetImage"
                android:layout_marginTop="-50dp"
                android:layout_gravity="center_horizontal"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/earth_transparent"
                />

        <TextView
                android:id="@+id/planetName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:textColor="@color/white"
                android:text="Mars"
                android:fontFamily="@font/assistant_extrabold"
                android:textSize="30dp"/>

        <TextView
                android:id="@+id/planetViews"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:fontFamily="@font/assistant_extrabold"
                android:text="155 views"/>

    </LinearLayout>


</LinearLayout>

图像被裁剪,我不知道如何预测。 我现在不关心色差,我只是想设置一些东西,所以它不会裁剪图像。

【问题讨论】:

  • 布局只显示在其边界内的视图。要实现您想要的,ImageView 需要位于内部布局之外。我建议查看ConstraintLayout 并将其用于整个视图层次结构。
  • 是的,谢谢,现在可以使用了

标签: android crop android-image


【解决方案1】:

把imageview放在外面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:background="@drawable/category_item_background"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:paddingBottom="20dp">

        <TextView
            android:id="@+id/planetName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="70dp"
            android:fontFamily="@font/assistant_extrabold"
            android:gravity="center_horizontal"
            android:text="Mars"
            android:textColor="@color/white"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/planetViews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/assistant_extrabold"
            android:gravity="center_horizontal"
            android:text="155 views" />

    </LinearLayout>

    <ImageView
        android:id="@+id/planetImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:background="@color/black"
        android:src="@drawable/earth_transparent" />

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2013-08-16
    • 2013-12-03
    • 2013-07-18
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多