简体   繁体   English

TextView 边框不出现

[英]TextView border doesn't appear

I wanted to make border for my TextView with these lines of xml code which I make in res/drawable.我想用我在 res/drawable 中制作的这些 xml 代码行为我的 TextView 制作边框。 But border did not appear.但是边框没有出现。

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <stroke android:width="4dip" android:color="#FAF9F9"/>
</shape>

and this is my activityMain.xml where textView is:这是我的 activityMain.xml,其中 textView 是:

<TextView
        android:background="@drawable/border_style"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="1. If you somehow found a way to extract all of the gold from the bubbling core of our lovely little planet, you would be able to cover all of the land in a layer of gold up to your knees."
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.96"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintVertical_bias="1.0" />

example border_bg.xml示例border_bg.xml

` `

<!-- View background color -->
<solid
    android:color="@color/background_color" >
</solid>

<!-- View border color and width -->
<stroke
    android:width="1dp"
    android:color="@color/border_color" >
</stroke>

<!-- The radius makes the corners rounded -->
<corners
    android:radius="2dp"   >
</corners>

` `

and set it as a background in your textview并将其设置为 textview 中的背景

android:background="@drawable/border_bg"

this is working for me:这对我有用:

<stroke
    android:width="2dp"
    android:color="#FFFFFF" />
<corners android:radius="20dp" />
<solid android:color="@android:color/transparent"/>

try to give boundary color little bit dark as your code is working totally fine as color is so lights it is barely visible so try with black color or any color you want and even after giving dark color also it is not working let me know .尝试给边界颜色一点点暗,因为你的代码工作得很好,因为颜色太亮了,几乎看不到所以尝试黑色或任何你想要的颜色,即使在给深颜色之后它也不起作用让我知道。 make change it your code like this像这样改变你的代码

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@android:color/white" />
<stroke android:width="4dip" android:color="#000000"/> /*change here android:color*/ 

</shape>

Border not Appear because you have give border color same as background color.边框不出现,因为您将边框颜色设置为与背景颜色相同。 give it different like below给它不同的像下面

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<stroke android:width="4dip" android:color="#000000"/>
</shape>

now set this xml as background of text view like below现在将此 xml 设置为文本视图的背景,如下所示

<TextView
    android:background="@drawable/border_style"
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:text="1. If you somehow found a way to extract all of the gold from the 
     bubbling core of our lovely little planet, you would be able to cover all of the 
     land in a layer of gold up to your knees."
    android:textSize="24sp"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.96"
    app:layout_constraintStart_toEndOf="@+id/imageView"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    app:layout_constraintVertical_bias="1.0" />

that its.!那它。!

you cannot see the border because it blends with your background color您看不到边框,因为它与您的背景颜色混合

a drawable like that will "work" for you to see像这样的可绘制对象将“工作”供您查看

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <stroke android:width="4dip" android:color="#FAF9F9"/>
</shape>

also consider adding a padding to your TextView with the same size as your border (so 4dp) android:padding="4dp"还可以考虑在 TextView 中添加与边框大小相同的填充(因此为 4dp) android:padding="4dp"

<TextView
        android:background="@drawable/border_style"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:padding="4dp"
        android:text="1. If you somehow found a way to extract all of the gold from the bubbling core of our lovely little planet, you would be able to cover all of the land in a layer of gold up to your knees."
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.96"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintVertical_bias="1.0" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM