Android 同时使用相对长度、线性布局和布局重量-奇怪的行为
我在一个XML文件中指定了一个活动布局-activity\u intro.XML,我正在尝试创建另一个类似但略有不同的布局-activity\u instructions.XML 介绍活动在屏幕底部有一个9patch图像,该图像应该保持在那里,并且只能根据屏幕的不同宽度进行调整。 “说明”活动应该包含相同的图像,但上面还有两个按钮-所有这三个视图都需要始终位于屏幕底部 activity_intro.xml:Android 同时使用相对长度、线性布局和布局重量-奇怪的行为,android,android-layout,android-linearlayout,android-relativelayout,android-xml,Android,Android Layout,Android Linearlayout,Android Relativelayout,Android Xml,我在一个XML文件中指定了一个活动布局-activity\u intro.XML,我正在尝试创建另一个类似但略有不同的布局-activity\u instructions.XML 介绍活动在屏幕底部有一个9patch图像,该图像应该保持在那里,并且只能根据屏幕的不同宽度进行调整。 “说明”活动应该包含相同的图像,但上面还有两个按钮-所有这三个视图都需要始终位于屏幕底部 activity_intro.xml: <?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/home_background" >
<LinearLayout
style="@style/Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/introAnimationImageView"
android:layout_width="152dip"
android:layout_height="176dip"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/intro_animation_content_description"
android:src="@drawable/animation_intro01" />
<TextView
android:id="@+id/introTextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intro_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/introTextViewSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intro_subtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/introButtonLoginSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/intro_button_label_login_signup" />
<Button
android:id="@+id/introButtonInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/introButtonLoginSignup"
android:text="@string/intro_button_label_instructions" />
<Button
android:id="@+id/introButtonReportAnonymously"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/introButtonLoginSignup"
android:layout_centerHorizontal="true"
android:text="@string/intro_button_label_report_anonymously" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:contentDescription="@null"
android:scaleType="fitXY"
android:src="@drawable/footer_cityscape" />
</LinearLayout>
结果:
因为我已经为Intro编写了工作代码,所以我想让指令遵循它的示例,但是layout_weight属性的行为并没有达到预期的效果。首先,我只是试着把2个按钮放进去,而不放图片:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.1"
android:background="@drawable/home_background" >
<LinearLayout
style="@style/Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/instructionsTextViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/instructions_title_1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<ImageView
android:id="@+id/instructionsImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/instructions_image_content_description"
android:src="@drawable/forms" />
<TextView
android:id="@+id/instructionsTextViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instructions_description_1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/instructionsButtonPrevious"
style="@style/ButtonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/instructions_button_label_previous" />
<Button
android:id="@+id/instructionsButtonNext"
style="@style/ButtonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/instructions_button_label_next" />
</RelativeLayout>
</LinearLayout>
这只有在我将底部RelativeLayout的布局权重设置为1(而不是0)和ScrollView的布局权重设置为0.1(而不是1)时才起作用。如果我使用原始值,RelativeLayout将占据整个屏幕。谁能给我解释一下原因吗?
我也试着在谷歌上搜索这个问题,注意到人们会建议我将layout_height设置为0dip,我试过了,但它也没有按预期工作
其次,我尝试将前面提到的ImageView添加到底部RelativeLayout。但是,这基本上只显示ImageView而不显示按钮,或者其中一个按钮位于图像顶部(隐藏它)。为什么呢?我不是特别设置了按钮放在它下面吗?
代码应该是什么样子的,才能实现我期望的功能
进一步解释:
下面是一些图片,它们应该有助于说明我到底想要实现什么。绿色部分是ScrollView——我添加它们是因为Android设备往往有不同的屏幕大小。其目的是独立于屏幕大小正确呈现内容,即如果屏幕较小,用户将能够滚动该部分以读取整个文本并查看图像。
左侧的红色位(简介)显示了应始终位于屏幕底部的ImageView;它总是在那里,可见,它上面的绿色部分是可移动的。
如果你看一下右边的红色部分(说明),就会看到一个下一步按钮,它覆盖了在简介屏幕截图中可见的卡车/卡车的图像。现在这是错误的-应该有2个按钮下面的图像,如上一个屏幕截图(2个蓝色矩形)
通常,在使用
layout\u weight
时,您将希望应用权重的轴上的尺寸设置为0
(因此您的滚动视图将使用高度0
)。在没有看到布局的情况下,很难说出你在做什么,关于最后一张图片。试试这个,你的意思是0dp。试试看。我在截图中添加了更多的解释-请看这是否有帮助。