Android XML: как разместить виджет под включенным макетом - PullRequest
0 голосов
/ 05 ноября 2019

Я пытаюсь разместить виджет диаграммы (com.android.AnyChartView, @ + id / piechart ) из библиотеки AnyChart (https://github.com/AnyChart/AnyChart-Android) под включенным макетом ( @). layout / content_main ), который содержит TabHost. Я попытался присвоить include идентификатор и установить свойство layout_below виджета диаграммы, но это не сработало. График приложения заполняет весь экран, покрываяпредставление вкладки.

Ниже приведен мой код:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"/>

    <include layout="@layout/content_main"
         />

    <com.anychart.AnyChartView
        android:id="@+id/piechart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:padding="5dp"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="5dp"
    android:background="#ffffff"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TabHost
        android:id="@+id/tab_host"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

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

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

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />
            </FrameLayout>
        </LinearLayout>
    </TabHost>




</androidx.constraintlayout.widget.ConstraintLayout>

Ниже приведен скриншот приложения: enter image description here

Ответы [ 2 ]

1 голос
/ 05 ноября 2019

Вам необходимо удалить свою круговую диаграмму из main_activity и поместить ее в content_main, например:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:padding="5dp"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        tools:context=".MainActivity"
        tools:showIn="@layout/activity_main">

        <TabHost
            android:id="@+id/tab_host"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ffffff"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

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

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">

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

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

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

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </TabHost>


        <com.anychart.AnyChartView
            android:id="@+id/piechart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toBottomOf="@+id/tab_host" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Я добавил scrollview, но вы можете удалить его, если его нет в вашемтребования.

0 голосов
/ 05 ноября 2019

Существует несколько возможных решений

Решение 1

В теге include укажите android:layout_width="match_parent" и android_height="wrap_content"

<include layout="@layout/content_main"
     android:layout_width="match_parent"
     android_height="wrap_content"
     />

Решение 2

Используя LinearLayout в качестве родителя ViewGroup, укажите android:layout_weight="1" и android:layout_height="0dp" в теге include, это позволит content_main.xmlзанимают оставшееся место и все же помещаются на экране.

<include layout="@layout/content_main"
     android:layout_width="match_parent"
     android_height="0dp"
     android_layout_weight="1"
  />
...