2017-07-18 28 views
1

我想將浮式動作按鈕兩個editTexts之間這樣的CardView內部之間對齊FAB: cardwithedittext如何將兩個EditTexts

我嘗試以下,但它不工作。

我該怎麼做?任何幫助將不勝感激。

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

使用相對佈局或限制佈局 – Pavan

回答

0

你可以根據你的需要這個 -

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

我已經改變了空白和邊距,設置它。

0

使用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> 

+0

您應該避免使用相對佈局儘可能,更不用說嵌套相對佈局。看到這個解釋 - https://stackoverflow.com/a/17496262/8039601 –

+0

是的,但它對初學者很容易。 –

+0

但是在這種情況下,嵌套的線性佈局比嵌套的相對佈局更簡單。看到我的答案。 –

0

嘗試這種使用線性佈局

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