Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中在LinearLayout内左右对齐2幅图像_Android_Android Layout - Fatal编程技术网

如何在android中在LinearLayout内左右对齐2幅图像

如何在android中在LinearLayout内左右对齐2幅图像,android,android-layout,Android,Android Layout,我想在谷歌地图下面并排显示两幅图像。图像显示正在工作。但是图像显示了相同的一面。我想在两张图片中间显示一些文字 我想在视图右侧显示的第二个图像,文本在中间 现行代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/t

我想在谷歌地图下面并排显示两幅图像。图像显示正在工作。但是图像显示了相同的一面。我想在两张图片中间显示一些文字

我想在视图右侧显示的第二个图像,文本在中间

现行代码

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

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/bt_white">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginStart="5dp"
            android:src="@drawable/ic_menu_camera"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView12"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:gravity="right"
            android:src="@drawable/ic_menu_camera" />

    </LinearLayout>


</LinearLayout>

用以下代码替换.xml

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

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/bt_white">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:layout_marginStart="5dp"
            android:src="@drawable/ic_menu_camera"/>

            <TextView
                android:layout_width="0dp"
                android:layout_height="80dp"
                android:gravity="center"
                android:layout_weight="1"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>

        <ImageView
            android:id="@+id/imageView12"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:gravity="right"
            android:src="@drawable/ic_menu_camera" />

    </LinearLayout>


</LinearLayout>

用以下代码替换.xml

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

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/bt_white">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:layout_marginStart="5dp"
            android:src="@drawable/ic_menu_camera"/>

            <TextView
                android:layout_width="0dp"
                android:layout_height="80dp"
                android:gravity="center"
                android:layout_weight="1"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>

        <ImageView
            android:id="@+id/imageView12"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:gravity="right"
            android:src="@drawable/ic_menu_camera" />

    </LinearLayout>


</LinearLayout>

添加重力
中心
权重总和
,并为
线性布局添加权重

 <LinearLayout
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity ="center" //--- add this
        android:weightSum =" 3" //--add this
        android:background="@color/bt_white">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_marginStart="5dp"
            android:layout_weight = "1" //---add this
            android:src="@drawable/ic_menu_camera"/>

        <LinearLayout
            android:layout_width="0dp" //--0
            android:layout_weight = "1" //---add this
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView12"
            android:layout_weight = "1" //---add this
            android:layout_width="0dp"  //-- make it to zero
            android:layout_height="80dp"
            android:gravity="right"
            android:src="@drawable/ic_menu_camera" />

    </LinearLayout>

添加重力
中心
权重总和
,并为
线性布局添加权重

 <LinearLayout
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity ="center" //--- add this
        android:weightSum =" 3" //--add this
        android:background="@color/bt_white">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_marginStart="5dp"
            android:layout_weight = "1" //---add this
            android:src="@drawable/ic_menu_camera"/>

        <LinearLayout
            android:layout_width="0dp" //--0
            android:layout_weight = "1" //---add this
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView12"
            android:layout_weight = "1" //---add this
            android:layout_width="0dp"  //-- make it to zero
            android:layout_height="80dp"
            android:gravity="right"
            android:src="@drawable/ic_menu_camera" />

    </LinearLayout>

/***您可以使用RelativeLayout而不是LinearLayout****/
/***您可以使用RelativeLayout而不是LinearLayout****/

您可以尝试使用RelativeLayout,如下所示-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ndroid:background="#fff">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="5dp"
            android:src="@drawable/ic_menu_camera" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/imageView1"
            android:layout_toStartOf="@+id/imageView2"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="5dp"
            android:src="@drawable/ic_menu_camera" />
    </RelativeLayout>
</LinearLayout>

您可以尝试使用RelativeLayout,如下所示-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ndroid:background="#fff">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="5dp"
            android:src="@drawable/ic_menu_camera" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/imageView1"
            android:layout_toStartOf="@+id/imageView2"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="@string/action_settings"/>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="5dp"
            android:src="@drawable/ic_menu_camera" />
    </RelativeLayout>
</LinearLayout>

试试这个:通过传递权重来完成

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_logo" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="center"
                android:text="@string/action_settings"
                android:textColor="@android:color/black" />

            <ImageView
                android:id="@+id/imageView12"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_logo" />

        </LinearLayout>

</LinearLayout>

试试这个:通过传递权重来完成

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_logo" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="center"
                android:text="@string/action_settings"
                android:textColor="@android:color/black" />

            <ImageView
                android:id="@+id/imageView12"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_logo" />

        </LinearLayout>

</LinearLayout>

这是您的布局

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

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bt_white"
    android:orientation="horizontal"
    android:weightSum="3">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="5dp"
        android:src="@android:drawable/ic_menu_camera" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="First Text View"
            android:textColor="@color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Second Text view"
            android:textColor="@color/black" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_menu_camera" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>

这是它的样子

这是您的布局

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

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bt_white"
    android:orientation="horizontal"
    android:weightSum="3">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="5dp"
        android:src="@android:drawable/ic_menu_camera" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="First Text View"
            android:textColor="@color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Second Text view"
            android:textColor="@color/black" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_menu_camera" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>

这是它的样子


我在Centery中有多个文本视图添加权重将更改图像的大小,我认为这是不可接受的。我在Centery中有多个文本视图添加权重将更改图像的大小,我认为这是不可接受的。欢迎:)很高兴有帮助添加权重将更改图像的大小,这是不可接受的我认为这是可选的,我们仍然可以使用重力将它们设置为中心,如果我们需要渲染图像的宽度,我们可以使用硬编码的宽度强制它们匹配欢迎:)很高兴它有帮助添加权重将改变图像的大小,这是不可接受的我认为这是可选的,我们仍然可以使用重力将它们设置为中心,如果我们使用正在渲染的图像的宽度,我们可以使用硬编码的宽度强制它们匹配。增加权重将改变图像的大小,这是不可接受的。我想我在检查此项之前已经回答了这个问题。我有两个答案。看看什么适合你的要求。增加重量会改变图像的大小,这是不可接受的,我想我在检查之前已经回答了。我有两个答案。看看什么适合你的要求。