-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!" /> 
相關問題