【问题标题】:How to add space between two textviews如何在两个文本视图之间添加空格
【发布时间】:2013-09-15 10:03:22
【问题描述】:

我是 android 开发的新手,我只是想知道如何在两个 TextView 之间添加空间?任何帮助将不胜感激。

到目前为止我写的代码

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lbl_group_coworkers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coworkers" />

    <TextView 
        android:id="@id/lbl_group_"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Family"/>



</LinearLayout>

【问题讨论】:

  • 将父 LinearLayout 替换为 RelativeLayout 并在您的第二个 textView 中添加 android:layout_marginLeft="10dp"
  • 是的,这也是 tj's 建议的简单方法。

标签: android android-layout


【解决方案1】:

在左上右下添加边距

 android:layout_marginLeft="10dp"

【讨论】:

    【解决方案2】:

    android:layout_marginRight="..."添加到第一个textView

    【讨论】:

      【解决方案3】:

      尝试添加边距

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >
      
      <TextView
          android:id="@+id/lbl_group_coworkers"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Coworkers" 
          android:layout_margin="10dp"/>
      
      <TextView 
      
          android:id="@+id/lbl_group"
          android:layout_margin="10dp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Family"/>
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        你可以使用

        android:layout_margin="5dp"

        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        

        但在您提出更多此类问题之前,我建议您阅读 android 开发指南 (http://developer.android.com/guide/components/fundamentals.html)

        祝你好运,玩得开心……

        【讨论】:

          【解决方案5】:

          你可以在两个文本视图之间留出边距。 为第二个文本视图添加上边距

          喜欢这个

           <TextView 
              android:id="@id/lbl_group_"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="20dp"
              android:text="Family"/>
          

          【讨论】:

            【解决方案6】:

            你可以使用 android:layout_marginTop="value" 像这样

            <?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="fill_parent"
                    android:orientation="vertical" >
            
                    <TextView
                        android:id="@+id/lbl_group_coworkers"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Coworkers" />
            
                    <TextView 
                        android:id="@id/lbl_group_"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="20dp"
                        android:text="Family"/>
                </LinearLayout>
            

            【讨论】:

              【解决方案7】:

              您可以根据需要使用填充或边距,这是一个人的链接,他在两者之间给出了很好的解释,可以帮助您决定要使用哪个:android margin vs padding

              【讨论】:

                【解决方案8】:

                只需替换&lt;LinearLayout&gt; &lt;/LinearLayout&gt;&lt;RelativeLayout&gt; &lt;/RelativeLayout&gt; 然后进入图形布局并根据需要调整空间。

                【讨论】:

                • 应该给出不特定于布局类型的解决方案。
                【解决方案9】:
                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   android:orientation="vertical" android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:background="@drawable/custom_border"
                  >
                  <TextView
                      android:text="@string/textView_reference_number"
                      style="@style/CustomTextView"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:id="@+id/textView_refernce_number_label"
                      />
                   <TextView
                      android:text="Reference Number Value"
                      style="@style/CustomTextView"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:id="@+id/textView_refernce_number"
                      android:layout_marginTop="10dp"/>   
                 </LinearLayout>
                

                android:layout_marginTop XML 属性用于指定此视图顶部的额外空间。因此,第二个 textView 上的 android:layout_marginTop="10dp" 指定了它与上方视图之间的 10 dp 空间。 @UDI 请参阅下面的链接以获取更多关于同一 https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html 的详细信息

                【讨论】:

                • 你能解释一下你的答案在做什么吗?简单地粘贴代码不会帮助任何人理解这个概念。
                • android:layout_marginTop XML 属性用于指定此视图顶部的额外空间。因此,第二个 textView 上的 android:layout_marginTop="10dp" 指定了它与上方视图之间的 10 dp 空间。 @UDI 请参阅下面的链接以获取更多详细信息。 developer.android.com/reference/android/view/…
                • 请检查。希望能帮助到你。如果您需要任何其他帮助或有任何其他困惑,请发表评论。 @UDID
                • 请添加此答案以供将来使用。
                【解决方案10】:

                在 TextView 中设置边距属性...

                android:layout_marginTop="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="20dp"
                

                如果所有边都设置空间那么...

                android:layout_margin="20dp"
                

                【讨论】:

                  【解决方案11】:

                  使用GridLayout 而不是LinearLayout

                  <GridLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">
                          <TextView
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:text="A"
                              android:layout_gravity="left" />
                          <TextView
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:text="B"
                              android:layout_gravity="right" />
                  </GridLayout>
                  

                  【讨论】:

                    猜你喜欢
                    • 2014-03-30
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-01-10
                    • 2017-07-18
                    • 2010-10-14
                    相关资源
                    最近更新 更多