Android 安卓布局间距
我是Android开发者的新手,很抱歉,这可能是一个基本问题 我试图让两个文本视图出现在屏幕的顶部,每个视图占用相同的空间,但是在两个视图之间以及屏幕的左侧和右侧有一个20dp的边距 很像 我目前的代码是:Android 安卓布局间距,android,layout,android-linearlayout,Android,Layout,Android Linearlayout,我是Android开发者的新手,很抱歉,这可能是一个基本问题 我试图让两个文本视图出现在屏幕的顶部,每个视图占用相同的空间,但是在两个视图之间以及屏幕的左侧和右侧有一个20dp的边距 很像 我目前的代码是: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_par
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:orientation="horizontal">
<TextView
android:id ="@+id/1_Heading"
android:text="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id ="@+id/2_Heading"
android:text="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
如果这是一个基本问题,我有什么想法吗?很抱歉,我已经尝试在网上找到解决方案,但没有多大帮助。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:weightSum="2"
android:orientation="horizontal">
<TextView
android:id ="@+id/1_Heading"
android:text="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id ="@+id/2_Heading"
android:text="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
您只需为第一个文本视图提供一半的右边距,为第二个文本视图提供一半的左边距,如下所示:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:orientation="horizontal">
<TextView
android:id ="@+id/1_Heading"
android:text="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp" />
<TextView
android:id ="@+id/2_Heading"
android:text="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
您只需为第一个文本视图提供一半的右边距,为第二个文本视图提供一半的左边距,如下所示:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:orientation="horizontal">
<TextView
android:id ="@+id/1_Heading"
android:text="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp" />
<TextView
android:id ="@+id/2_Heading"
android:text="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
尝试使用此布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/heading1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#4CAF50"
android:padding="5dp"
android:text="1"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/heading2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#4CAF50"
android:padding="5dp"
android:text="2"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
</LinearLayout>
尝试使用此布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/heading1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#4CAF50"
android:padding="5dp"
android:text="1"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/heading2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#4CAF50"
android:padding="5dp"
android:text="2"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
</LinearLayout>
试试这个:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:layout_weight="100"
android:orientation="horizontal">
<TextView
android:id ="@+id/Heading_1"
android:text="1"
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id ="@+id/Heading_2"
android:text="2"
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
结果:试试这个:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:layout_weight="100"
android:orientation="horizontal">
<TextView
android:id ="@+id/Heading_1"
android:text="1"
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id ="@+id/Heading_2"
android:text="2"
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
结果:- 在第一个文本视图上给出android:layout_marginEnd=“10dp”
- 在第二个文本视图中给出android:layout_margintart=“10dp”
- 在第一个文本视图上给出android:layout_marginEnd=“10dp”
- 在第二个文本视图中给出android:layout_margintart=“10dp”
您可以通过为父按钮(LinearLayout)提供20dp的填充,然后从结束到第一个按钮提供10dp的边距,从开始到第二个按钮提供10dp的边距来处理此问题
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:padding="20dp"
android:orientation="horizontal">
<TextView
android:text="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginEnd="10dp"
/>
<TextView
android:text="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginStart="10dp"
/>
您可以通过为父级(LinearLayout)提供20dp的填充,然后从末端到第一个按钮提供10dp的边距,从开始到第二个按钮提供10dp的边距来处理此问题
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:padding="20dp"
android:orientation="horizontal">
<TextView
android:text="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginEnd="10dp"
/>
<TextView
android:text="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:background="#4CAF50"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginStart="10dp"
/>
那么,您当前的布局有什么问题?您是否尝试使用“匹配父项”而不是“0dp”?对于第一个文本,使用alos LAYOU marginRight=“20d”那么,当前布局有什么问题?您是否尝试使用“匹配父项”而不是“0dp”?对于第一个文本,使用alos layout_marginRight=“20d”看起来不错-尽管我认为我的主要问题是模拟器没有正确显示东西!不过现在一切都好了。如何开始第二行的2个文本视图?创建一个新的LinearLayout将其方向设置为垂直将上述LinearLayout粘贴两次,并为所有文本视图提供唯一id,但这些id似乎不起作用:(我只得到了前2个,在NeathSorted下没有显示任何其他内容-需要更改布局\u顶部线性布局的高度以包装匹配的内容\u父项将父项线性布局高度设置为'wrapcontent'看起来不错-尽管我认为我的主要问题是模拟器没有正确显示内容!但现在都已排序。H如何开始第二行的2个文本视图?创建一个新的LinearLayout将其方向设置为垂直,将上面的LinearLayout粘贴两次,并为所有文本视图提供唯一id,该id似乎不起作用:(我只得到前2个,NeathSorted下没有显示其他内容-需要更改顶部linearlayout的布局高度以将内容从匹配项包装到父项将父项linearlayout高度设置为“wrapcontent”