Pre-Lollipop上的FAB位置错误

时间:2017-04-15 13:32:03

标签: android android-layout android-edittext floating-action-button

我在EditText旁边使用FAB ... 在棒棒糖上,它正常工作,即完全在EditText旁边,但在Pre-Lollipop上,它显示相当附着,有点在下方......

这是截图 - https://i.stack.imgur.com/1QVtF.jpg

这是我在片段布局中使用的代码 -

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:wheel="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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_id"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8.0dip"
        android:layout_marginLeft="10.0dip"
        android:layout_marginRight="95.0dip">

        <AutoCompleteTextView
            android:textColorHint="#ffb7b7b7"
            android:id="@+id/editText1"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:hint="Enter Username"
            android:ems="10"
            android:singleLine="true"
            android:inputType="textNoSuggestions">

            <requestFocus/>

        </AutoCompleteTextView>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/hit"
        app:backgroundTint="?attr/colorAccent"
        app:rippleColor="#64B5F6"
        android:layout_marginLeft="250dp"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

问题是,您为250dpTextInputLayout使用了固定宽度AutoCompleteTextView。还使用了FloatingActionButton固定left-margin作为250dp

以下是用于在所有XML版本中显示same layout的有效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"
    xmlns:wheel="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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/hit"
        app:backgroundTint="?attr/colorAccent"
        app:rippleColor="#64B5F6"
        android:layout_alignParentRight="true"
        android:layout_marginTop="8dp"/>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/button1"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="16dp">

        <AutoCompleteTextView
            android:textColorHint="#ffb7b7b7"
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Username"
            android:ems="10"
            android:lines="1"
            android:inputType="textNoSuggestions">

            <requestFocus/>
        </AutoCompleteTextView>
    </android.support.design.widget.TextInputLayout>

</RelativeLayout>

<强>输出:

enter image description here

希望这会有所帮助〜

答案 1 :(得分:0)

这是工作的XML文件。试试这个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"
    xmlns:wheel="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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/hit"   
        app:backgroundTint="?attr/colorAccent"
        app:rippleColor="#64B5F6"
        android:layout_alignTop="@+id/til_id"
        android:layout_toRightOf="@+id/til_id"
        android:layout_toEndOf="@+id/til_id" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <AutoCompleteTextView
            android:textColorHint="#ffb7b7b7"
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Username"
            android:ems="10"
            android:text="trial"
            android:singleLine="true"
            android:inputType="textNoSuggestions">

            <requestFocus/>

        </AutoCompleteTextView>

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

希望这会很好..