【问题标题】:Attempting to place an ImageView between two text views?试图在两个文本视图之间放置一个 ImageView?
【发布时间】:2017-02-20 03:42:28
【问题描述】:

我试图在 2 个文本视图之间放置两个图像视图,但是当我在我的 Android 设备上通过一个或两个文本视图运行它时,我的图像要么消失,要么似乎被“裁剪”,当我尝试运行时它。有人可以告诉我为什么会这样吗?

<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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.annagib.rileycard.Main"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fontFamily="cursive"
        android:text="Riley Rayne"
        android:textColor="#000000"
        android:textSize="40sp" />

    <ImageView
        android:id="@+id/rye"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/chomie" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0"
        android:fontFamily="cursive"
        android:text="Poet. Writer. Innovator"
        android:textColor="#000000"
        android:textSize="40sp" />
</LinearLayout>

【问题讨论】:

  • 您已将所有视图的高度设置为匹配父级,这基本上与您的图像视图重叠
  • 在两个 TextView 中使用 wrap_content。并将 adjustViewBounds 和 wrap_content 放入 ImageView

标签: android xml user-interface


【解决方案1】:

试试这个,

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="3"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fontFamily="cursive"
            android:text="Riley Rayne"
            android:textColor="#000000"
            android:textSize="40sp" />

        <ImageView
            android:id="@+id/rye"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/chomie"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fontFamily="cursive"
            android:text="Poet. Writer. Innovator"
            android:textColor="#000000"
            android:textSize="40sp" />
    </LinearLayout>

【讨论】:

    【解决方案2】:

    您应该将所有 textView 和图像视图的高度和宽度更改为

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

    并且导致问题的 textview 尺寸太大。您应该将其大小更改为 small.以及 atrribs

        android:layout_width="match_parent"
        android:layout_height="match_parent"
    

    负责视图的重叠

    另一种方法是赋予元素权重。并将高度设为0dp

    即。

    <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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.annagib.rileycard.Main"
    tools:showIn="@layout/activity_main">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fontFamily="cursive"
        android:textSize="40sp"
        android:layout_weight="1"
        android:textColor="#000000"
        android:text="Riley Rayne" />
    
    <ImageView
        android:id="@+id/rye"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/chomie"/>
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fontFamily="cursive"
        android:layout_weight="1"
        android:textSize="40sp"
        android:textColor="#000000"
        android:text="Poet. Writer. Innovator" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多