Организовать просмотр изображений в середине верхнего макета - PullRequest
0 голосов
/ 24 мая 2018

У меня есть такой макет с пользовательским верхним заголовком.Я хочу, чтобы ImageView был в середине заголовка, то есть center_horizontal.Я попытался установить гравитацию и гравитацию для макетов, но иногда это нормально.Но когда я попробовал приложение в альбомном режиме, логотип переместился на одну сторону, и изображение не в правильном центре макета.

<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="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/phoneoptLayout"
android:background="@mipmap/bgg"
android:paddingBottom="5dp"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="6dp">

<LinearLayout
    android:id="@+id/top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="left"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:paddingTop="10dp"
        android:src="@drawable/arrow_back" />

    <ImageView android:src="@mipmap/logo"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:layout_marginRight="30dp"
        android:layout_gravity="center_horizontal" />

</LinearLayout>
</LinearLayout>

А в другом макете у меня есть кнопка выхода справа отзаголовокЭто тоже не в центре.Я новичок в Android, поэтому я не знаю, что это исправить.

Ответы [ 6 ]

0 голосов
/ 24 мая 2018

Вы можете попробовать с приведенным ниже кодом.

<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="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/phoneoptLayout"
android:background="@mipmap/bgg">

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/back"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="left"
            android:layout_centerVertical="true"
            android:src="@drawable/arrow_back" />

        <ImageView 
            android:src="@mipmap/logo"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

    </RelativeLayout>
</RelativeLayout>
0 голосов
/ 24 мая 2018

Сделать высоту и ширину LinearLayout, чтобы они соответствовали родительскому и центру тяжести

    <LinearLayout
     android:id="@+id/top"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_gravity="center">
0 голосов
/ 24 мая 2018

сделайте вашу линейную ширину "match_parent" такой, как эта

 <LinearLayout
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
0 голосов
/ 24 мая 2018

вы использовали android:layout_centerHorizontal="true" в своем LinearLayout.вы получите свой LinearLayout в середине топ макета.

Например:

<?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:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/phoneoptLayout"
    android:background="@mipmap/ic_launcher"
    android:paddingBottom="5dp"
    android:orientation="vertical"
    android:paddingLeft="5dp"
    android:paddingRight="6dp">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="left"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:paddingTop="10dp"
            android:src="@drawable/send_video" />

        <ImageView android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:layout_marginRight="30dp"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>
</RelativeLayout>
0 голосов
/ 24 мая 2018

Замените линейный макет на относительный макет.Также у вас есть верхняя относительная компоновка, но вы закрыли линейную компоновку.

0 голосов
/ 24 мая 2018

Надеюсь, это сработает для вас.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/phoneoptLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="6dp">

    <ImageView
        android:id="@+id/back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/ic_launcher" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginRight="30dp"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>
...