【问题标题】:How to align FAB between two EditTexts如何在两个 EditTexts 之间对齐 FAB
【发布时间】:2017-07-19 13:01:19
【问题描述】:

我想将浮动操作按钮放在 CardView 内的两个 editText 之间,如下所示: cardwithedittext

我尝试了以下方法,但它不起作用。

我该怎么做?任何帮助将不胜感激。

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="25dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="204dp"
                android:layout_height="wrap_content"
                android:hint="Enter origin address" />

        </LinearLayout>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:onClick="fabClick"
            android:src="@mipmap/ic_add_white_24dp"
            app:fabSize="normal"
            app:layout_anchor="@id/fab_coordinator_layout"
            app:layout_anchorGravity="left|end" />


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter destination address" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >


        </LinearLayout>


    </LinearLayout>
</android.support.v7.widget.CardView>

【问题讨论】:

  • 使用相对布局或约束布局

标签: android android-layout alignment


【解决方案1】:

你可以这样做-

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="25dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter origin address" />
            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter destination address" />
        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:onClick="fabClick"
            android:src="@drawable/ic_add"
            app:fabSize="normal"
            android:layout_gravity="center_vertical"
            app:layout_anchor="@id/fab_coordinator_layout" />

    </LinearLayout>
</android.support.v7.widget.CardView>

我已经改变了padding和margin,根据你的需要设置。

【讨论】:

    【解决方案2】:

    如下使用RelativeLayout

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="5dp">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:id="@+id/inputHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/fabHolder"
            android:layout_toStartOf="@+id/fabHolder"
            android:orientation="vertical">
    
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    
        <RelativeLayout
            android:id="@+id/fabHolder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/inputHolder"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/inputHolder">
    
            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:src="@android:drawable/ic_menu_send" />
        </RelativeLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 你应该尽可能避免使用相对布局,更不用说嵌套的相对布局了。请参阅此进行解释 - stackoverflow.com/a/17496262/8039601
    • 是的,但是对初学者来说很容易。
    • 但是在这种情况下,嵌套的线性布局比嵌套的相对布局更简单。看我的回答。
    【解决方案3】:

    使用线性布局试试这个

        <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/tools"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        card_view:cardCornerRadius="25dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your hint" />
    
                <EditText
                    android:id="@+id/etDestination"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Your hint" />
            </LinearLayout>
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/btnFindPath"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="fabClick"
                android:src="@drawable/ic_down"
                app:fabSize="normal"
                android:layout_gravity="center"
                app:layout_anchor="@id/fab_coordinator_layout" />
    
        </LinearLayout>
    </android.support.v7.widget.CardView>
    

    【讨论】:

      猜你喜欢
      • 2013-12-14
      • 1970-01-01
      • 2015-03-25
      • 2014-10-21
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多