设置为toRightOf时会显示布局,但设置为toLeftOf时会显示布局

时间:2015-09-24 14:12:56

标签: android android-relativelayout floating-action-button

我正在尝试使用FAB的标签进行布局,但是我的RelativeLayout出了问题。第二个RelativeLayout仅在设置为第一个的右侧时出现,而在设置为左侧时不出现。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".StartAndLogActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_start_and_log" />

<RelativeLayout
    android:id="@+id/RelativeLayout_ActionButtonAndLabelContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom">

    <RelativeLayout
        android:id="@+id/RelativeLayout_FloatingActionButtonContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/FAB_NewBankPurchase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_new_bank_purchase" />


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/FAB_NewTrade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/FAB_NewBankPurchase"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_trade" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/FAB_ShowMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/FAB_NewTrade"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="@dimen/fab_margin"
            android:layout_marginLeft="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_show_menu" />

    </RelativeLayout>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/RelativeLayout_FloatingActionButtonContainer">
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HI"
            android:textColor="@color/colorAccent"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>

</RelativeLayout>

这会产生:

enter image description here

我的其他代码(不起作用)是这样的:     

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_start_and_log" />

<RelativeLayout
    android:id="@+id/RelativeLayout_ActionButtonAndLabelContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom">

    <RelativeLayout
        android:id="@+id/RelativeLayout_FloatingActionButtonContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/FAB_NewBankPurchase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_new_bank_purchase" />


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/FAB_NewTrade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/FAB_NewBankPurchase"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_trade" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/FAB_ShowMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/FAB_NewTrade"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="@dimen/fab_margin"
            android:layout_marginLeft="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_show_menu" />

    </RelativeLayout>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/RelativeLayout_FloatingActionButtonContainer">
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HELLO, HI"
            android:textColor="@color/colorAccent"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>

</RelativeLayout>

此代码产生:

enter image description here

感谢您的时间和帮助。

3 个答案:

答案 0 :(得分:1)

这是我的解决方案:

  <RelativeLayout
android:id="@+id/RelativeLayout_ActionButtonAndLabelContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
android:id="@+id/RelativeLayout_FloatingActionButtonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:layout_gravity="right|bottom">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/FAB_NewBankPurchase"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="16dp" 
    android:layout_marginTop="0dp"
    android:src="@drawable/ic_new_bank_purchase" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/FAB_NewTrade"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/FAB_NewBankPurchase"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="16dp" 
    android:layout_marginTop="0dp"
    android:src="@drawable/ic_trade" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/FAB_ShowMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/FAB_NewTrade"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="@dimen/fab_margin" 
    android:layout_marginTop="0dp"
    android:src="@drawable/ic_show_menu" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/FAB_ShowMenu">
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="HELLO, HELLO, HELLO, HELLO,  HELLO, HELLO, HELLO, HELLO, HELLO, HI"

        android:layout_alignParentTop="true"/>
</RelativeLayout>

这意味着您的解决方案正常运行,但您无法在左侧看到TextView,因为&#34;没有空格&#34;,因为父布局的宽度设置为& #34; WRAP_CONTENT&#34;

答案 1 :(得分:0)

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/FAB_NewBankPurchase"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="16dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp" />

</RelativeLayout>

一个简短的例子。 RelativeLayout的每个元素都有相对的!位置。所以使用一个相对父母

答案 2 :(得分:0)

好的问题是它的工作但你无法看到它超出显示器

尝试将First Relative布局设置为右边,然后设置你的好处,使用

 <RelativeLayout
        android:id="@+id/RelativeLayout_FloatingActionButtonContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="right|bottom">
//First Layout contents
</RelativeLayout>
<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_toLeftOf="@id/RelativeLayout_FloatingActionButtonContainer">
//Second Layout contents
</RelativeLayout>