Android 如何使用LinearLayout定位按钮死点
我试图将文本视图下方的按钮设置为屏幕的死角。Android 如何使用LinearLayout定位按钮死点,android,button,android-linearlayout,center,Android,Button,Android Linearlayout,Center,我试图将文本视图下方的按钮设置为屏幕的死角。 目前,我只将它们设置为水平中心,而不是垂直中心,因此按钮位于TextView的正下方,这不是我想要的。 我甚至试过用重力代替重力,但还是没有运气 这是我得到的 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fi
目前,我只将它们设置为水平中心,而不是垂直中心,因此按钮位于TextView的正下方,这不是我想要的。
我甚至试过用重力代替重力,但还是没有运气 这是我得到的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<Button android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="INFO"
android:id="@+id/info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
</LinearLayout>
按如下方式更新布局:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<TextView android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<Button android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_below="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_below="@+id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="INFO"
android:id="@+id/info"
android:layout_below="@+id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
</RelativeLayout></RelativeLayout>
按如下方式更新布局:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<TextView android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<Button android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_below="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_below="@+id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
<Button android:text="INFO"
android:id="@+id/info"
android:layout_below="@+id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
/>
</RelativeLayout></RelativeLayout>
您可以使用RelativeLayout作为父对象,并将
android:layout\u centerInParent=“true”
添加到您的线性布局
中,该布局将包装按钮。此外,LinearLayout
的宽度和高度应设置为wrap\u content
您可以实现以下结果:
使用了以下xml:
<RelativeLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<Button android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button android:text="INFO"
android:id="@+id/info"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
最后只是一个建议:
使用LinearLayout
match_parent
而不是fill_parent
,因为它是不推荐使用的,因为API 8您可以使用RelativeLayout作为父级,并向您添加包装按钮的android:layout\u centerInParent=“true”
。此外,LinearLayout
的宽度和高度应设置为wrap\u content
您可以实现以下结果:
使用了以下xml:
<RelativeLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<Button android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button android:text="INFO"
android:id="@+id/info"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
最后只是一个建议:
使用LinearLayout
match\u parent
而不是fill\u parent
,因为它是不推荐使用的,因为API 8我真的不能忍受嵌套布局,对不起。因此,我必须提出一个替代方案
<RelativeLayout
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"
tools:context=".MainActivity"
>
<TextView
android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
<!-- All you need is a little trick -->
<!-- This dummy View is invisible, but it's CENTERED -->
<!-- The 4 Buttons will be positioned in RELATION to this one -->
<!-- This is the power of RelativeLayouts -->
<View
android:id="@+id/vwDummy"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent"true"
/>
<!-- These 2 are horizontally centered and go above the dummy -->
<Button
android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_above="@id/vwDummy"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_above="@id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<!-- These 2 are horizontally centered and go below the dummy -->
<Button
android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_below="@id/vwDummy"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:text="INFO"
android:id="@+id/info"
android:layout_below="@id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
如评论中所示,我利用了亲戚孩子的相对性。你所需要的只是一个小把戏:
- 在中心创建一个不可见的虚拟视图李>
- 将4个按钮与此按钮对齐
- 我真的不能忍受嵌套布局,对不起。
因此,我必须提出一个替代方案
<RelativeLayout
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"
tools:context=".MainActivity"
>
<TextView
android:text="CALCULATOR"
android:id="@+id/appTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
<!-- All you need is a little trick -->
<!-- This dummy View is invisible, but it's CENTERED -->
<!-- The 4 Buttons will be positioned in RELATION to this one -->
<!-- This is the power of RelativeLayouts -->
<View
android:id="@+id/vwDummy"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent"true"
/>
<!-- These 2 are horizontally centered and go above the dummy -->
<Button
android:text="BASIC ALGEBRA"
android:id="@+id/alg"
android:layout_above="@id/vwDummy"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:text="BASIC MATH"
android:id="@+id/basicMath"
android:layout_above="@id/alg"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<!-- These 2 are horizontally centered and go below the dummy -->
<Button
android:text="BASIC CALCULUS"
android:id="@+id/calc"
android:layout_below="@id/vwDummy"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:text="INFO"
android:id="@+id/info"
android:layout_below="@id/calc"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
如评论中所示,我利用了亲戚孩子的相对性。你所需要的只是一个小把戏:
- 在中心创建一个不可见的虚拟视图李>
- 将4个按钮与此按钮对齐
,并使用android:layout\u centerInParent
属性将其居中放置到true
中。您是否有任何模型表示您想要的最终视图是什么?您是否可以定义“死点”?在第一个线性布局中,将高度设置为与父级android:layout\u height匹配=“匹配你的父母“谢谢大家的支持,我从来不知道应该避免使用嵌套布局——我会在将来的程序中记住这一点。然而,由于这对我来说并不是一个严肃的项目,我想我会坚持使用@L-X答案,因为它对我来说是最简单的修复方法。再次感谢!您可以尝试将按钮包装成一个
,并使用android:layout\u centerInParent
属性将其居中放置到true
。您是否有任何模型表示您想要的最终视图是什么?您是否可以定义“死点”?在第一个线性布局中,将高度设置为匹配父android:layout\u height=“匹配你的父母“谢谢大家的支持,我从来不知道应该避免使用嵌套布局——我会在将来的程序中记住这一点。然而,由于这对我来说并不是一个严肃的项目,我想我会坚持使用@L-X答案,因为它对我来说是最简单的修复方法。再次感谢!您可以去掉嵌套的LinearLayout,因为它确实不需要。您是对的。由于性能问题,应避免嵌套布局。但我相信这不会导致性能问题。我认为这样更容易理解,因为提问者在xml中也使用了线性布局。请随意发布其他解决方案。您可以摆脱嵌套的LinearLayout,因为它确实不需要。您是对的。由于性能问题,应避免嵌套布局。但我相信这不会导致性能问题。我认为这样更容易理解,因为提问者在xml中也使用了线性布局。请随意发布其他解决方案。您可以删除嵌套的RelativeLayout,因为它确实不需要。您可以删除嵌套的RelativeLayout,因为它确实不需要。