Поместите два вида в центр экрана - PullRequest
0 голосов
/ 07 февраля 2020

У меня есть два представления (com.github.mikephil.charting.charts.BarChart и LinearLayout), содержимое одного представления имеет высоту около 600 dp, а другое - 500 dp. Ширина обоих видов - fill_parent.

Я бы хотел расположить их обоих в центре экрана (чтобы они перекрывались).

К сожалению, выравнивание компоновки Android очень не интуитивно понятно и ударить или пропустить меня, и я не могу этого достичь.

Не могли бы вы помочь?

               <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:background="#11FFFFFF">

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/chart"
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:gravity="center">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="50dp"
                        android:orientation="vertical"
                        android:gravity="center">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#FFFFFF"
                            android:textSize="150dp"
                            android:includeFontPadding="false"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:text="HITS"
                            android:textAppearance="?android:attr/textAppearanceLarge"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="50dp"
                        android:orientation="vertical"
                        android:gravity="center">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#FFFFFF"
                            android:textSize="150dp"
                            android:includeFontPadding="false"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:text="MISSED"
                            android:textAppearance="?android:attr/textAppearanceLarge"/>
                    </LinearLayout>
                </LinearLayout>

            </RelativeLayout>

Я хочу расположить оба Представления внутри этого RelativeLayout в центре экран, перекрывающийся

Ответы [ 3 ]

2 голосов
/ 07 февраля 2020

Поместите 2 вида, которые вы хотите разместить в центре внутри RelativeLayout, затем добавьте android:layout_centerInParent="true" в 2 вида.

Например, это выглядит так:

<RelativeLayout  
     android:layout_width="match_parent"
     android:layout_height="match_parent">
       <com.github.mikephil.charting.charts.BarChart
            ....all other stuff

            android:layout_centerInParent="true" > //..add this 


      <LinearLayout
           ..... all other stuff
           android:layout_centerInParent="true"  > //..add this 


      </LinearLayout>

</RelativeLayout>
0 голосов
/ 07 февраля 2020

По моему опыту, в большинстве случаев рекомендуется использовать самый легкий контейнер, который в данном случае, вероятно, равен FrameLayout, поэтому я бы использовал его вместо RelativeLayout и использовал android:layout_gravity="center" в подпредставлениях, таких как это например:

<FrameLayout  
     android:layout_width="match_parent"
     android:layout_height="match_parent">
       <com.github.mikephil.charting.charts.BarChart
            android:layout_gravity="center" > 

      <LinearLayout
            android:layout_gravity="center" >
      </LinearLayout>

</FrameLayout>
0 голосов
/ 07 февраля 2020

Определите ваш основной макет как ConstraintLayout, а затем ограничьте все стороны обоих элементов своим родителем. Примерно так:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

        <LinearLayout
            android:layout_width="600dp"
            android:layout_height="600dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">


        </LinearLayout>


        <com.github.mikephil.charting.charts.BarChart
            android:layout_width="500dp"
            android:layout_height="500dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">


        </com.github.mikephil.charting.charts.BarChart>

</android.support.constraint.ConstraintLayout>

С этим кодом ваша BarChart выше вашей LinearLayout.

...