【问题标题】:Android, fit an image and textview into the screenAndroid,将图像和文本视图放入屏幕
【发布时间】:2016-05-16 17:21:36
【问题描述】:

我添加了一个带有 ImageView 的片段,它是一个 PNG 图像,并在其下方添加了一个 TextView。 片段显示正常,但是当我旋转屏幕时,图像将占据大部分屏幕,并且 TextView 被部分剪切。 代码如下:

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

<ImageView
    android:id="@+id/shopping_cart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/shopping_cart" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_below="@id/shopping_cart"
    android:text="@string/no_data"
    android:gravity="center"/>


</RelativeLayout>

我正在尝试找到一种方法来调整图像的大小,以便在屏幕上同时显示图像和文本

【问题讨论】:

    标签: android android-layout android-fragments android-imageview


    【解决方案1】:

    如果您想保留相同的图像大小而不是调整图像大小,请尝试使用滚动视图作为根元素。这是一个小sn-p -

    <ScrollView
        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"
        android:fillViewport="true">
    
    
    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
    (all your view elements)
    
    </RelativeLayout>
    

    【讨论】:

    • 是的,实际上它可能是一种选择。谢谢你的主意。如果我无法调整图像大小,我将使用您的解决方案
    【解决方案2】:
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
    
    <LinearLayout
        android:id="@+id/text_image_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
    (Enter view here)
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      好的,所以首先确保您拥有所有密度的 ImageView 图像。我建议您在 ImageView 代码中使用此属性:

      https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

      缩放类型定义了您的图像应如何根据视图进行缩放。我曾经遇到过类似的问题,我能够使用它来解决它。话虽如此,我还没有看到你使用的图像,所以我不能很确定。但是,我会说你使用:

      android:scaleType="centerCrop"
      

      您可以在上面提供的链接上找到该属性的定义。另外,我不知道您的应用程序的目标设备是什么,但是将视图保留在滚动视图中总是好的,这样即使在较小的屏幕上也可以看到它们。

      试一试,如果不是通过 centerCrop,也可能是通过其他属性。

      干杯!!

      【讨论】:

        【解决方案4】:

        代码如下:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/shopping_text"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="@string/no_data"/>
        
        <ImageView
            android:id="@+id/shopping_cart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/shopping_text"
            android:src="@drawable/shopping_cart"/>
        
        
        </RelativeLayout>
        

        【讨论】:

        • 这不起作用。如果您尝试代码甚至无法正确对齐图像
        • 我假设你的整个屏幕只包含 imageview 和 textview,我们希望 textView 在 ImageView 下面,我假设这些项目在屏幕中心对齐。我能够实现所有这些。如果你能上传图片,我可以试试看。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 2011-12-24
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多