2016-11-29 65 views
0

我一直在争取了两天,尝试在Android中创建自定义布局,分为3部分,一个“投票”计数器容器,显​​示投票人数消息,一个文本消息容器,其中将包含文本和消息的作者以及将用于对消息进行投票的按钮容器。Android:容器XML布局包装重量和填充高度

到目前为止,我有这样的代码

<?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填补其上位容器的整个高度。

  • 将容器右侧的作者名称对齐,但仅在“消息”TextView的末尾对齐。

我一直在试图解决问题,修复改变containerText容器的高度FILL_PARENT没有成功,改变一路攀升到主容器中的所有height属性FILL_PARENT。

如果我尝试使用alignParentRight = true将作者姓名对齐到右侧,那么RelativeLayout将占据整个屏幕空间,并推出按钮容器。我也尝试改变我将不同布局尊重他人的方式,改变toLeftOf或toRightTo,并带来可怕的结果。

任何帮助将不胜感激,因为我坚持这个问题。

干杯!

回答

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

插入 '机器人:layout_alignParentRight = “真”' 使得TextView的以填充整个宽度 - >给这个输出:http://imgur.com/a/a5EkB 删除layout_below使其显示在消息的右侧,就好像布局不能垂直增长一样。 – PayToPwn

+0

您是否添加了alignParentBottom?它应该坚持到容器的底部 –

+0

是的,我改变了layout_height中的match_parent并添加了alignParentBottom和alignParentRight,但是我得到了我在最后一条评论中发布的图像。 – PayToPwn