-6

这是我的布局XML:为什么我的TextView在LinearLayout中无边框显示?

而且

<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秒之间,但我期待的边界围绕着TextView的文字本身。我如何做到这一点?

+0

可能只使用'wrap_content'的高度属性在你的第二个TextView – convexHull

+0

从视图中删除它:android:background =“@ drawable/border”并为其提供颜色,它将起作用。 –

回答

0

只需在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> 
0

试试这个

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

更改您的布局文件:

<?xml version="1.0" encoding="utf-8"?> 

<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="wrap_content" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/border" 
    xmlns:android="http://schemas.android.com/apk/res/android" /> 

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

有根据的设计裕你的TextView。

+0

请详细解答。 –

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

,如果你在TextView的应用填充和删除观Textviews之间,将做工精细,试试这个代码....它的工作

<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!" /> 
相关问题