【问题标题】:Why does my TextView show up without borders in a LinearLayout?为什么我的 TextView 在 LinearLayout 中显示为无边框?
【发布时间】:2017-05-30 05:30:57
【问题描述】:

这是我的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@drawable/border" />

    <TextView android:text="ahahah 2!"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border"
        xmlns:android="http://schemas.android.com/apk/res/android" />

</LinearLayout>

这是边界定义:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@android:color/white" />
    <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

这会使TextViews 之间出现一条水平线,但我希望TextViews 本身的文本周围有一个边框。我该如何做到这一点?

【问题讨论】:

  • 可能只使用wrap_content 作为第二个TextView 的高度属性
  • 从 View 中删除它:android:background="@drawable/border" 并为其提供颜色,它将起作用。

标签: android android-layout android-linearlayout textview


【解决方案1】:

只需在Text View 本身中定义它。

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    android:text="Hello World!" />

更改您的 xml 代码

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:background="@drawable/border"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="Hello World!"
        android:textSize="22sp" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@drawable/border"
        android:text="Hello World!"
        android:textSize="22sp" />


</LinearLayout>

【讨论】:

    【解决方案2】:

    试试这个

    <?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="match_parent"
    android:padding="5dp"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@drawable/border" />
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:text="ahahah 2!" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:
      <?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="match_parent"
          android:orientation="vertical">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginTop="10dp"
              android:background="@drawable/border"
              android:text="Hello World!"
              android:textSize="22sp" />
      
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginTop="10dp"
              android:background="@drawable/border"
              android:text="Hello World!"
              android:textSize="22sp" />
      
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:
                <TextView android:layout_width="wrap_content" 
                 android:layout_height="wrap_content" 
                 android:background="@drawable/border" 
                 android:text="desired text"
                 android:layout_margin="5dp"/>
        

        试试这个背景xml

                 <?xml version="1.0" encoding="utf-8"?>
                  <shape 
                   xmlns:android="http://schemas.android.com/apk/res/android" 
                    android:shape="rectangle">
        
                   <stroke android:color="@color/colorPrimary" 
                    android:width="1dp"/>
                   <solid android:color="@color/colorPrimary"/>
                   <corners android:radius="5dp"/>
                 </shape>
        

        根据设计让您的文本视图带有边距。

        【讨论】:

        • 请详细回答。
        【解决方案5】:

        如果您在 Textview 上应用填充并删除 Textviews 之间的 View 可以正常工作,请尝试此代码....它正在工作

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/border"
            android:layout_marginTop="20dp"
            android:padding="5dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="ahahah 2!" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-06
          • 2021-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多