ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – RelativeLayout作为listview项目

2019-07-09 01:28:08  阅读:141  来源: 互联网

标签:android android-listview relativelayout


考虑将RelativeLayout作为列表视图项:

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="bigfoo"
        android:textSize="60sp"/>

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:layout_centerVertical="true"
        android:text="foo"/>

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/foo"
        android:layout_alignLeft="@id/foo"
        android:text="bar"/>

</RelativeLayout>

在使用hierarchyviewer(在使用Android JB / API 17的设备上)调查它之后,bar获得0高度.

编辑:预期结果:

问题:这种相对布局行为的解释是什么,以及
如何修复布局以实现布局,满足要求:foo位于bigfoo的中间(垂直),而bar位于foo之上?

解决方法:

我看到两个选项:

选项1,没有嵌套(这种方式栏位于布局的顶部,但如果使用布局,则可以设置一个上边距以降低它):

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="bigfoo"
        android:textSize="60sp"/>

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:layout_centerVertical="true"
        android:text="foo"/>

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:text="bar"/>

</RelativeLayout>

选项2,在第一个中嵌套另一个RelativeLayout:

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="bigfoo"
        android:textSize="60sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/bigfoo"
        android:layout_alignBottom="@id/bigfoo"
        android:orientation="vertical"
        android:layout_toRightOf="@id/bigfoo">

        <TextView
            android:id="@+id/foo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="foo" />

        <TextView
            android:id="@+id/bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/foo"
            android:text="bar" />
    </RelativeLayout>
</RelativeLayout>

使用选项2,您应该完全达到您期望的结果,但代价是将布局嵌套在另一个布局中.如果UI的其余部分很轻,那应该不是问题;)

编辑:你能试试吗?

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="bigfoo"
        android:textSize="60sp"/>

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:layout_centerVertical="true"
        android:text="foo"/>

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/foo"
        android:layout_alignLeft="@id/foo"
        android:text="bar"/>

</RelativeLayout>

这就是我所看到的,虽然我很难解释为什么这样有效……

标签:android,android-listview,relativelayout
来源: https://codeday.me/bug/20190709/1407617.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有