我是Android新手,我的应用程序终于可以工作了,但是我在布局上遇到了问题。
下面的xml
被剥离了,但我标记它的地方<repeats>
我有 4 个水平布局。
每个都有不同的小部件Spinner
、SeekBar
和RadioButtons
都用分隔线分隔开来。
每个设置都选择一个设置,然后按右侧的Set
按钮。
我的问题,虽然所有小部件都整齐地左对齐,但按钮的垂直对齐到处都是。
如果我只是玩小部件边距,它将不适合不同的 Android 设备。
如何在不从左侧移动所有其他内容的情况下使Buttons
垂直向右排列?
GETTING WANT
------------------------------- --------------------------------
| <Spinner> <button> | | <Spinner> <button> |
| < -------- Divider --------> | | < -------- Divider --------> |
| <RadioButton > <button> | | <RadioButton> <button> |
| < -------- Divider --------> | | < -------- Divider --------> |
| <SeekBar> <button> | | <SeekBar> <button> |
| < -------- Divider --------> | | < -------- Divider --------> |
| <RadioButton> <button> | | <RadioButton> <button> |
------------------------------- --------------------------------
这是我的相关代码:
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
tools:scrollY="?android:attr/scrollbarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<repeats> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/dynamic_TZ_spinner"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp20"
android:layout_marginRight="@dimen/dp20" />
<Button
android:id="@+id/TZ_btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set" />
</LinearLayout>
<View
android:id="@+id/divider3"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="@dimen/dp10"
android:background="?android:attr/listDivider"
android:visibility="visible" />
....
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
请尝试这种方式并注意,当您想将小部件放在其父项的右侧或左侧时,请使用" android:layout_alignParentRight="true" "或" android:layout_alignParentLeft="true" ">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_vertical"
tools:scrollY="?android:attr/scrollbarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/dynamic_TZ_spinner"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="@dimen/dp20" />
<Button
android:id="@+id/TZ_btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/dp20"
android:text="@string/set" />
</RelativeLayout>
<View
android:id="@+id/divider3"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="10dp"
android:background="?android:attr/listDivider"
android:visibility="visible" />
....
</LinearLayout>
</RelativeLayout>
您可以使用子项中的FrameLayout
和layout_gravity
属性来实现它。
对于一行使用:
<FrameLayout
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:text="Radio Button" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:text="Button" />
</FrameLayout>
试试这个:-
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
tools:scrollY="?android:attr/scrollbarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<repeats> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/dynamic_TZ_spinner"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/dp20"
android:layout_marginRight="@dimen/dp20" />
<Button
android:id="@+id/TZ_btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/set" />
</RelativeLayout>
<View
android:id="@+id/divider3"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="@dimen/dp10"
android:background="?android:attr/listDivider"
android:visibility="visible" />
....
</LinearLayout>
</RelativeLayout>
您可以为每个小部件和按钮(列(使用线性布局