Android Studio 如何并排设置图像视图和文本视图?

6qqygrtg  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(304)

我打算把imageViewtextView并排放在一个cardView中。它在设计时看起来很好,但当我运行应用程序时表现得很奇怪。
我的item_offer_list.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginTop="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="8dp">

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

        <ImageView
            android:id="@+id/cv_iv_photo_poster"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="null"
            tools:src="@drawable/ic_launcher_background" />

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_toEndOf="@+id/cv_iv_photo_poster"
            android:gravity="center"
            android:text="@string/app_name">

        </TextView>
    </LinearLayout>

</androidx.cardview.widget.CardView>

我在回收站视图中使用了上述设计,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".OfferWallListFragment">

    <LinearLayout
        android:id="@+id/llRecyclerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/pbLoadingItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />

        <TextView
            android:id="@+id/txtNoConnection"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:visibility="gone"
            tools:text="No Internet Connection" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_OfferData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:visibility="visible"
            tools:listitem="@layout/item_offer_list">

        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我的设计看起来很好,如下图:

但是,当我在模拟器中运行应用程序时,我的图像非常大,但覆盖了所有空间,如下所示:

为什么会这样,我不知道?如果有什么想法,请调查一下。

ijnw1ujt

ijnw1ujt1#

我想你是从互联网上下载图片。也许你忘了在你的item_offer_list.xml中指定ImaveView的大小?
我的意思不是这样的:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

请尝试以下操作:

android:layout_width="60dp"
android:layout_height="60dp"

或您自己的尺寸,而不是60dp

sqserrrh

sqserrrh2#

这里有两个选项:
1.首先可以调整位图图像的大小,然后进行设置。
整数最大高度= 2000;整数最大宽度= 2000;
最小浮点数= Math.min(((浮点数)最大高度/位图.getWidth()),((浮点数)最大宽度/位图.getHeight()));
矩阵matrix = new Matrix();(比例,比例);
如果是,则创建一个新的位图。
1.使用了
布局宽度=“ Package 内容”布局高度=“ Package 内容”

相关问题