Android View 问题 - ImageView 和两个 TextView

标签 android view textview imageview

我试图将两个 TextView 放置在 ImageView 的右侧(注意这不在列表中)。我关注了this示例有效,但当我开始增加 TextView 的文本大小和背景时出现问题。尽管将高度设置为 wrap_content,但顶部 TextView 似乎在扩展。

看例子不太明白

android:layout_height="?android:attr/listPreferredItemHeight"

以及为什么布局高度设置为26dip

android:layout_height="26dip"

这是我的 xml,对 layout_height 做了一些我认为可行的小改动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"

    android:padding="6dip">

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" android:layout_marginRight="6dip"

        android:src="@drawable/icon" />

    <TextView android:id="@+id/secondLine" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_toRightOf="@id/icon"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" android:background="@layout/rounded"
        android:textSize="20sp" android:singleLine="true" android:textColor="#FFFFFF"
        android:textStyle="bold" android:ellipsize="marquee" android:padding="10dip"
        android:layout_margin="5dip"
        android:text="Simple application that shows how to use RelativeLayout" />

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="@layout/rounded"
        android:layout_toRightOf="@id/icon" android:textSize="20sp"
        android:singleLine="true" android:textColor="#FFFFFF"
        android:textStyle="bold" android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" android:layout_above="@id/secondLine"
        android:padding="10dip" android:layout_margin="5dip" android:gravity="center_vertical"
        android:text="My Application" />

</RelativeLayout>

这就是生成的图像???

enter image description here

如有任何帮助,我们将不胜感激。

我偶然发现了 this post这似乎有效

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:padding="6dip">
    <ImageView android:id="@+id/Icon" android:layout_margin="5dip"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_centerVertical="true" android:src="@drawable/addphoto" />
    <TextView android:id="@+id/topLine" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:layout_margin="4dip"
        android:layout_toRightOf="@+id/Icon" android:background="@layout/rounded"
        android:padding="10dip" android:textSize="20sp" android:singleLine="true"
        android:textColor="#FFFFFF" android:textStyle="bold" android:text="Name" />
    <TextView android:id="@+id/bottomLine" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:layout_margin="4dip"
        android:layout_toRightOf="@+id/Icon" android:layout_below="@+id/topLine"
        android:padding="10dip" android:background="@layout/rounded"

        android:textSize="20sp" android:singleLine="true" android:textColor="#FFFFFF"
        android:textStyle="bold" android:text="Address" />

</RelativeLayout>

最佳答案

您看到该结果是因为您在 TextView 上具有以下属性:android:layout_alignParentTop="true"android:layout_above="@id/secondLine"

上述属性指定顶部必须与父组件对齐,底部必须位于“secondline”组件的正上方。

我删除了上述两个属性并对您的布局做了一些更改,以达到我认为您想要的效果。请参阅下面的布局。我希望这个布局能达到你想要的效果。

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

android:padding="6dip">

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"

    android:src="@drawable/icon" />

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/icon"
    android:layout_below="@+id/text"
    android:layout_alignParentRight="true"
    android:textSize="20sp"
    android:singleLine="true"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:ellipsize="marquee"
    android:padding="10dip"
    android:layout_margin="5dip"
    android:text="Simple application that shows how to use RelativeLayout" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/icon"
    android:layout_alignBaseline="@id/icon"
    android:textSize="20sp"
    android:singleLine="true"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:padding="10dip"
    android:layout_margin="5dip"
    android:gravity="center_vertical"
    android:text="My Application" />

关于Android View 问题 - ImageView 和两个 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6658154/

相关文章:

java - 作为计数器,以定时间隔将值从 1 美元增加到 125 万美元

android - 检测点击父项时点击了哪个子项

Android ListView 拉动刷新和滑动列表项以显示按钮

机器人 : CurrentLocation always return null value

android - Android应用程式在安装时当机

python - 在 Django 中将变量从模板传递到 View 的更有效方法是什么?

java android 路径动画

android - 单击 TextView 超链接时出错

javascript - 在 Backbone 中删除和取消绑定(bind) subview

Android Textview平均分配水平方向