【问题标题】:Android: prevent background color from changing toward the edge of a shapeAndroid:防止背景颜色向形状边缘变化
【发布时间】:2016-06-15 07:52:22
【问题描述】:

我需要创建带有红色边框(宽度1px)的白色矩形形状,并从下方附加另一个红色矩形。像这样:

白色矩形将是EditText,而红色边框和红色矩形将出现以通知用户输入错误。没有什么花哨。

我已经尝试了几种方法,但由于相同的原因,它们都不好看 - 颜色向形状边缘变化(看起来最外面的像素是变化的像素)。这个问题可以在这张放大的图片中看到:

这个特定快照的层次结构是红色背景的LinearLayout1pxpadding,但我在尝试使用其他方法时也观察到了相同的效果(包括以编程方式在EditText 周围绘制边框) .

这个效果看起来像Android的标准,我的问题是如何禁用它?

【问题讨论】:

    标签: android user-interface colors drawable shapes


    【解决方案1】:

    查看以下示例,希望对您有所帮助。

    rect_shape.xml

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

    layout.xml

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle_shape"
            android:orientation="vertical">
    
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:lines="3"
                android:padding="5dp"/>
    
            <View
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="#EF9A9A"/>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="36dp"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#EF5350"/>
    
    
        </LinearLayout>
    

    输出:

    【讨论】:

    • 这与我需要的完全相反——我想要纯色、单一的颜色,没有任何可区分的“边框”
    • 嗨 Vasiliy,请准确解释您想要什么,我没有得到。
    【解决方案2】:

    以防万一有人偶然发现这个问题 - 所描述的行为是一些与模拟器相关的工件。在真实设备上,颜色刚刚好。

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 2022-09-27
      • 1970-01-01
      • 2012-03-13
      • 2015-10-23
      • 2017-09-03
      相关资源
      最近更新 更多