如何在所有设备的同一行(基线)中对齐fab编辑文本和图像视图

时间:2017-09-28 18:07:42

标签: android xml layout

我是Android的新手,我正在尝试开发一个应用程序,我在线性布局自定义编辑文本和图像视图以及相对布局FAB和谷歌广告,你在图像This is what i am trying to get for all device中看到,但当我我正在使用小的我正在得到另外的东西,我在第二张图片中添加This what I am getting in small device请帮我实现所有设备的图像1。在此先感谢任何帮助对我有帮助。

这是XML代码: -

 <RelativeLayout 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:orientation="vertical"
    tools:context="com.example.hp.newcode.MainActivity">

 <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/edittextxml"
        android:orientation="horizontal"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="13dp"
        android:layout_marginRight="13dp"
        android:layout_above="@+id/adView"
        android:layout_toLeftOf="@+id/btnQR"
        android:layout_gravity="bottom">
        <EditText
            android:layout_toLeftOf="@+id/search"
            android:id="@+id/inputSearchEditText"
            android:layout_width="220dp"
            android:paddingLeft="15dp"
            android:layout_height="match_parent"
            android:ellipsize="start"
            android:hint="Search Society"
            android:inputType="number"
            android:maxLength="10"
            android:textSize="15sp"
            android:background="@drawable/edittextxml"/>
        <LinearLayout
            android:id="@+id/imageLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:background="#275766">
            <ImageView
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:clickable="true"
                android:src="@drawable/search"/>
        </LinearLayout>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnQR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_action_name1"
        android:scaleType="center"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="9dp"
        android:layout_alignBottom="@+id/linearlayout"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我在代码中做了一些更改。希望它有所帮助: 我稍微改变了视图层次结构。 对于editText,我添加了android:layout_weight="1"android:layout_width="0dp,因此它可以响应所有屏幕尺寸。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="ltr"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginLeft="13dp"
        android:layout_marginRight="13dp"

        android:orientation="horizontal">

        <EditText
            android:id="@+id/inputSearchEditText"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:ellipsize="start"
            android:hint="Search Society"
            android:inputType="number"
            android:maxLength="10"
            android:paddingLeft="15dp"
            android:textSize="15sp" />

        <ImageView
            android:id="@+id/search"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="20dp"
            android:clickable="true" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnQR"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|end"
            android:layout_marginEnd="9dp"
            android:layout_marginRight="20dp"
            android:scaleType="center"
            app:fabSize="normal" />

    </LinearLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />
</LinearLayout>