Android XML:如何将小部件放置在包含的布局下方

我正在尝试将AnyChart库(https://github.com/AnyChart/AnyChart-Android)中的图表窗口小部件(com.android.AnyChartView, @ + id / piechart )放在包含的布局( @ layout / content_main ),其中包含一个TabHost。我尝试给include提供一个ID,并设置了图表小部件的layout_below属性,但这没有用。应用程序图会填充整个屏幕,并覆盖Tab视图。

下面是我的代码:

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>

以下是该应用的屏幕截图:

Android XML:如何将小部件放置在包含的布局下方

mafanmafan_elsa 回答:Android XML:如何将小部件放置在包含的布局下方

您需要像这样从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,但是如果您不符合要求,可以将其删除。

,

有许多可能的解决方案

解决方案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作为父ViewGroupandroid: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"
  />
本文链接:https://www.f2er.com/3160322.html

大家都在问