Android/Layout-理解按钮为何不可用的问题';t在这页的底部
我已经为我正在开发的测试程序实现了一个简单的布局。屏幕上只有一个徽标图像,一个带有程序标题的文本视图和一个按钮。我尝试使用线性布局,因为我希望每个元素都在一条新行中。在我的例子中,图像和文本可能非常接近,但我希望按钮出现在页面底部。我不明白我做错了什么。我还将android:layoutGravity属性添加到带有“bottom”值的按钮中,但没有任何更改。这是我正在使用的代码:Android/Layout-理解按钮为何不可用的问题';t在这页的底部,android,android-layout,Android,Android Layout,我已经为我正在开发的测试程序实现了一个简单的布局。屏幕上只有一个徽标图像,一个带有程序标题的文本视图和一个按钮。我尝试使用线性布局,因为我希望每个元素都在一条新行中。在我的例子中,图像和文本可能非常接近,但我希望按钮出现在页面底部。我不明白我做错了什么。我还将android:layoutGravity属性添加到带有“bottom”值的按钮中,但没有任何更改。这是我正在使用的代码: <ScrollView xmlns:android="http://schemas.android.com/a
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0sp"
tools:context=".DiLand"
android:orientation="vertical" >
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:src="@drawable/logo2" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/titleApp"
android:textColor="@color/titleColor"
android:textSize="35sp"
android:gravity="top"
android:layout_marginTop="0sp"
/>
<Button
android:layout_width="140sp"
android:layout_height="50sp"
android:text="@string/startButton"
android:id="@+id/start_button"
android:background="@drawable/start_button"
android:textColor="@color/whiteColor"
android:textSize="30sp"
android:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
谢谢你的帮助 这是线性布局,因此它不会将所有sapce分配给您,只会按顺序分配给您。请尝试以下代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0sp"
tools:context=".DiLand"
android:orientation="vertical" >
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:src="@drawable/logo2" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/titleApp"
android:textColor="@color/titleColor"
android:textSize="35sp"
android:gravity="top"
android:layout_marginTop="0sp"
/>
<View
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
</View>
<Button
android:layout_width="140sp"
android:layout_height="50sp"
android:text="@string/startButton"
android:id="@+id/start_button"
android:background="@drawable/start_button"
android:textColor="@color/whiteColor"
android:textSize="30sp"
android:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
它是线性布局,因此它不会将所有sapce分配给您,它只会按顺序分配给您。请尝试以下代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0sp"
tools:context=".DiLand"
android:orientation="vertical" >
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:src="@drawable/logo2" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/titleApp"
android:textColor="@color/titleColor"
android:textSize="35sp"
android:gravity="top"
android:layout_marginTop="0sp"
/>
<View
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
</View>
<Button
android:layout_width="140sp"
android:layout_height="50sp"
android:text="@string/startButton"
android:id="@+id/start_button"
android:background="@drawable/start_button"
android:textColor="@color/whiteColor"
android:textSize="30sp"
android:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
将LinearLayout更改为RelativeLayout,包括:
android:orientation="vertical"
因此,布局仍在每个视图的新行上,并在按钮上添加:
android:layout_alignParentBottom="true"
这样,按钮将位于屏幕/布局的底部。确保ScrollView的父视图填充整个屏幕
编辑:
添加到imageView:
android:layout_above="@+id/title"
添加到文本视图:
android:layout_above="@+id/start_button"
android:layout_below="@+id/logo"
添加到按钮:
android:layout_lower=“@+id/title”
现在尝试并编写。将LinearLayout更改为RelativeLayout,使用:
android:orientation="vertical"
因此,布局仍在每个视图的新行上,并在按钮上添加:
android:layout_alignParentBottom="true"
这样,按钮将位于屏幕/布局的底部。确保ScrollView的父视图填充整个屏幕
编辑:
添加到imageView:
android:layout_above="@+id/title"
添加到文本视图:
android:layout_above="@+id/start_button"
android:layout_below="@+id/logo"
添加到按钮:
android:layout_lower=“@+id/title”
现在就尝试并编写。从中删除Scrollview,它正在产生问题,并替换下面的代码。我已经测试了它的工作情况
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0sp"
>
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:gravity="top"
android:text="Title"
android:textColor="#ffffff"
android:textSize="35sp" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</View>
<Button
android:id="@+id/start_button"
android:layout_width="140sp"
android:layout_height="50sp"
android:layout_gravity="bottom|center_horizontal"
android:text="Start"
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
将Scrollview从中删除,它正在产生问题,并将下面的代码替换为它的工作状态,我已经测试过了
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0sp"
>
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0sp"
android:gravity="top"
android:text="Title"
android:textColor="#ffffff"
android:textSize="35sp" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</View>
<Button
android:id="@+id/start_button"
android:layout_width="140sp"
android:layout_height="50sp"
android:layout_gravity="bottom|center_horizontal"
android:text="Start"
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
原因是忽略了
滚动视图
的子级的匹配父级
/填充父级
高度。如果它得到尊重,则滚动视图
将毫无用处,因为它的内容和滚动视图窗口将具有相同的高度
您可以在
ScrollView
上设置android:fillViewPort=“true”
属性,使ScrollView
子视图在较小时扩展到其父级高度。如果子对象比父对象高,则此属性无效。通过这种方式,您可以将按钮始终放在底部,同时将其保留在滚动内容中。原因是匹配父对象
/填充父对象
忽略了滚动视图
子对象的高度。如果它得到尊重,则滚动视图
将毫无用处,因为它的内容和滚动视图窗口将具有相同的高度
您可以在
ScrollView
上设置android:fillViewPort=“true”
属性,使ScrollView
子视图在较小时扩展到其父级高度。如果子对象比父对象高,则此属性无效。通过这种方式,您可以使按钮始终位于底部,同时将其保留在滚动内容中。当然,我也尝试将其粘贴到布局xml文件中,但没有任何更改。请参阅,我发布了另一个答案及其工作原理,我已经测试过了。当然,我也尝试过将它粘贴到我的布局xml文件中,但没有任何更改。请看,我已经发布了另一个答案及其工作原理,我已经测试过了。为什么要使用scrollview?对于低清晰度的旧设备,页面可能会变得太大,无法在整个屏幕上停留。为什么要使用scrollview?对于低清晰度的旧设备,页面可能会变得太大,无法在整个屏幕上停留。我尝试了您的建议,但现在所有元素都被删除了重叠。我不明白是否必须为每个元素添加视图标记…我尝试了您的建议,但所有元素现在都重叠了。我不明白我是否必须为每个元素添加一个视图标记…谢谢它正在工作!我只有一个问题。对于屏幕分辨率较低的旧设备,我可以做什么?有可能他们在没有滚动视图的情况下看不到按钮…添加滚动视图,但将按钮放在它外面。不要担心它将由设备本身管理。如果答案有用,请更正答案并投票。@pratik感谢您的澄清。最后一个问题。为什么需要查看标记?事实上,它是为哪个动机而需要的?当您声明LinearLayout时,它将仅根据您所在的组件分配空间。LinearLayout不会分配更多空间。如果你想这样做,那么你必须使用相对布局。谢谢它的工作!我只有一个问题。对于屏幕分辨率较低的旧设备,我可以做什么?有可能他们在没有滚动视图的情况下看不到按钮…添加滚动视图,但将按钮放在它外面。不要担心它将由设备本身管理。如果答案有用,请更正答案并投票。@pratik感谢您的澄清。最后一个问题。为什么需要查看标记?事实上,它是为哪个动机而需要的?当您声明LinearLayout时,它将仅根据您所在的组件分配空间。LinearLayout不会分配更多空间。如果要这样做,则必须使用相对布局。