Android视图问题——ImageView和两个TextView

发布于 2024-11-19 13:38:21 字数 3761 浏览 1 评论 0原文

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

看例子不太明白

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>

这是结果图像???

在此处输入图像描述

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

我刚刚偶然发现这篇文章似乎有效

<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>

I'm trying to position two TextView's to the right of an image view (note this is not in a list). I followed this example which works but problems occur when I start to increase the text size and background of the TextView. Here the top text view seems to expand despite setting the height to wrap_content.

Looking at the example I don't really understand

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

and why the layout height is set to 26dip

android:layout_height="26dip"

Here is my xml with the minor changes to layout_height which I thought would work

<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>

And this is the resulting image???

enter image description here

Any help would be appreciated.

I just stumbled on this post which seems to work

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2024-11-26 13:38:21

您看到该结果是因为文本视图上具有以下属性: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" />

You see that result because you have the following attributes on the textview: android:layout_alignParentTop="true" and android:layout_above="@id/secondLine".

The above attributes specify that the top must be aligned with the parent and the bottom must be just above the 'secondline' component.

I removed the above two attributes and did a few more changes to your layout to get to what I believe you want. See the layout below. I hope this layout achieves what you wish to obtain.

<?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" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文