Android:容器XML布局包装重量和填充高度

时间:2016-11-29 14:43:26

标签: android android-layout android-xml

我一直在努力争取在Android中创建一个自定义布局,分为3个部分,一个"投票"计数器容器,显​​示投票的人数,包含文本的文本消息容器和消息的作者以及用于向上或向下投票的按钮容器。

到目前为止,我有这段代码

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

    <!-- # VOTES CONTAINER -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/containerVotes"
        android:layout_centerVertical="true"
     >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/votesPost"
            android:textStyle="bold"
            android:textSize="16sp"
        />
    </RelativeLayout>

    <!-- MESSAGE CONTAINER -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/containerText"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@+id/containerVotes"
        android:background="@color/gray"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:id="@+id/titleGlobus"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/AutorGlobus"
            android:textColor="#000000"
            android:textSize="12sp"
            android:textStyle="italic"
            android:maxWidth="180dp"
            android:layout_weight="0.1"
            android:layout_below="@+id/titleGlobus"
            />

    </RelativeLayout>

    <!-- VOTE UP/DOWN CONTAINER -->

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/containerButtons"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/containerText"
        android:layout_toEndOf="@+id/containerText"
        >

        <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/containerButtonUp"
            >

            <at.markushi.ui.CircleButton
                android:layout_width="32dip"
                android:layout_height="32dip"
                android:src="@mipmap/ic_arrow_drop_up_black_24dp"
                app:cb_color="@color/white"
                app:cb_pressedRingWidth="8dip"
                android:id="@+id/upvote_button"
                />

         </RelativeLayout>

        <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/containerButtonDown"
            android:layout_below="@+id/containerButtonUp"
            >

            <at.markushi.ui.CircleButton
                android:layout_width="32dip"
                android:layout_height="32dip"
                app:cb_color="@color/white"
                app:cb_pressedRingWidth="8dip"
                android:src="@mipmap/ic_arrow_drop_down_black_24dp"
                android:id="@+id/downvote_button"
                />

        </RelativeLayout>

    </RelativeLayout>

我使用上述代码在手机中获取的布局是:

Pre

我想实现这个目标:

Post

所以我需要在这里实现两件主要的事情:

  • 使RelativeLayout containerText 填充其高级容器的整个高度。

  • 将作者姓名下移到容器右侧,但在&#34;消息的末尾对齐&#34; TextView的。

我一直在尝试修复修复问题,将 containerText 容器的高度更改为fill_parent而没有成功,将所有高度属性一直更改为主容器fill_parent。

如果我尝试使用alignParentRight = true将作者姓名对齐,则RelativeLayout会占据屏幕的整个空间,然后推出按钮容器。我也尝试改变我定位不同布局的方式,更改toLeftOf或toRightTo,结果很糟糕。

任何帮助都会受到高度赞赏,因为我遇到了这个问题。

干杯!

1 个答案:

答案 0 :(得分:0)

试试这个:

<!-- MESSAGE CONTAINER -->
    <RelativeLayout
        android:layout_width="wrap_content"

        <!-- Set this height  to match_parent -->
        android:layout_height="match_parent"

        android:id="@+id/containerText"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@+id/containerVotes"
        android:background="@color/gray"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:id="@+id/titleGlobus"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/AutorGlobus"
            android:textColor="#000000"
            android:textSize="12sp"
            android:textStyle="italic"
            android:maxWidth="180dp"
            android:layout_weight="0.1"

            <!-- Removed below tag and added alignParentBottom and alignParentRight -->
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            />

    </RelativeLayout>