我使用导航抽屉活动创建了一个新的Android Studio项目。
然后我在content_main.xml中的LinearLayout中添加了一个简单的TextView。
对于这个LinearLayout(也尝试使用标准的RelativeLayout)我添加了
android:animateLayoutChanges="true"
参数。
如果我现在在GONE和VISIBLE之间切换TextView的可见性,则content_main.xml的整个根LinearLayout会被推高并隐藏在工具栏下。
这是我的content_main.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity"
android:background="#84d8ff">
<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="207dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0004"
android:animateLayoutChanges="true">
<TextView android:text="Lorem Ipsum..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/test_item"
android:layout_marginTop="29dp" />
</LinearLayout>
这是一张图片:
蓝色背景属于content_main.xml的根LinearLayout,绿色背景属于app_bar_main.xml,其中包含content_main.xml
有谁知道为什么会这样,以及如何解决它?
答案 0 :(得分:0)
你找到了一行:
<include layout="@layout/content_main" />
将其更改为:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom">
<include layout="@layout/content_main" />
</LinearLayout>
这是一个丑陋的修复,但它现在有效。
如果另一个问题引发,您可以通过将其添加为rootlayout的直接子项来修复它:
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v4.widget.Space>