Android 为什么我的文本视图在线性布局中显示为无边框?

Android 为什么我的文本视图在线性布局中显示为无边框?,android,android-layout,android-linearlayout,textview,Android,Android Layout,Android Linearlayout,Textview,这是我的布局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">

这是我的布局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>


这使得
TextView
s之间出现一条水平线,但我希望
TextView
s本身的文本周围有一个边框。如何实现这一点?

只需在
文本视图中定义它本身

 <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>

试试这个

<?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>


试试这个背景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>

让您的文本视图具有符合设计的边距


<?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>

如果在Textview上应用填充,并在两个Textview之间删除视图,效果会很好,请尝试以下代码。。。。它起作用了

<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!" />


可能只需在第二个文本视图中使用高度属性的
wrap\u content
,将其从视图中删除:android:background=“@drawable/border”,并为此提供颜色即可。请详细说明答案。
<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!" />