Android 线性布局中图元间距的最佳做法
我创建了一个简单的Android 线性布局中图元间距的最佳做法,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我创建了一个简单的线性布局,其中包含三个相同的元素: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="
线性布局
,其中包含三个相同的元素:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello"/>
</LinearLayout>
现在我将介绍每对元素之间的8dp
空间
以下哪种溶液被认为更清洁
或:
或者其他什么?试试这个
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
<android.support.v4.widget.Space
android:layout_width="8dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
<android.support.v4.widget.Space
android:layout_width="8dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
</LinearLayout>
为代码添加空间
<android.support.v4.widget.Space
android:layout_width="8dp"
android:layout_height="wrap_content" />
我喜欢已经发布的
空间
解决方案,但我想在原始问题的精神中添加另一个答案
在OP呈现的情况下,如果我要使用边距来完成此任务,我会在每个元素上使用开始/左侧边距,而不是第一个元素。我之所以这么做,是因为我预测未来布局可能会发生变化。在我看来,最有可能发生的事情是在
LinearLayout
的末尾添加新元素,或者从LinearLayout
的末尾删除元素。在这些情况下,如果我在每个项目上使用开始/左边距,我可以删除单个视图,而不必接触其他视图。在给定的解决方案中,您也可以为线性布局使用可绘制分隔符。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:showDividers="middle"
android:divider="@drawable/divider8p"
android:orientation="vertical">
<!-- Your items here -->
</LinearLayout>
对于分割声明:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#00FFFFFF"/>
<size android:width="8dp" android:height="8dp"/>
</shape>
如何使用视图并将其放置在视图之间?@MohammadZarei您的意思是这里描述的解决方案:我建议使用第二个解决方案:您可能希望将来在某个自定义类中更改TextView
,您可以将其嵌入该类中。或者,您可能希望为该TextView
创建style
资源,并在style
中定义边距。