带有椭圆形角的自动完成文本框

3
我希望创建一个自动完成文本视图的形状,如图所示,并且文本应该居中显示在其中。
目前我正在尝试实现它:
<item>
    <shape android:shape="rectangle" >
        <corners android:radius="15dip" />

        <solid android:color="@color/blue_text" />

        <padding
            android:bottom="5dip"
            android:left="15dip"
            android:right="15dip"
            android:top="5dip" />
    </shape>
</item>

<AutoCompleteTextView
  android:id="@+id/autoComp"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:gravity="center"
  android:background="@drawable/bg_autocompletetext"
  android:textColor="@color/white"
</AutoCompleteTextView>

但这只会使文本框的角变圆,而不是完全圆形。请提供任何可行的解决方案。


你可以将autoTextview的高度设置为48dp(默认高度),然后为形状提供24dp的圆角。这就是我在项目中所做的。移除高度的wrapcontent属性。 - Illegal Argument
使用9-patch背景。 - S.D.
@IllegalArgument,它不是那样工作的。 - Nouman Bhatti
5个回答

2
尝试这个形状的代码。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"    
  android:shape="rectangle" >

   <corners
    android:radius="100dp"/>
   <solid
    android:color="#2137FF"/>
   <size
    android:width="250dp"
    android:height="60dp"/>
</shape> 

这里输入图像描述

尝试使用这个工具


你是在问如何生成同样的形状并且想要什么吗?请查看更新后的答案(工具)。 - MilapTank
意思是我只得到了老化的结果,只有角落被圆滑了。但我想要左右两侧完全圆滑。 - Nouman Bhatti
分享你想要的图片。 - MilapTank

0

尝试这个,适用于圆角文本视图

<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_textview.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#284A89" />
    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
</shape>

0

您可以编写一个独立的 XML 布局文件(shape.xml),其中包含以下代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <solid android:color="#DCDCDC" />    
<stroke android:width="2.5dp" 
android:color="#DCDCDC" />
<corners
android:radius="10dp"/>
</shape>

然后在您的主要布局中添加:

<AutoCompleteTextView
android:id="@+id/autoComp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/shape"
android:textColor="@color/white"
</AutoCompleteTextView>

希望这能有所帮助。

0

最好使用9 patch图像作为TextView的背景。

使用9 patch图像,您可以为不同分辨率的设备设置适当的分辨率。这将给您带来更好的结果。


0

在drawable文件夹中添加以下文件 "searchtextbox_border.xml"

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners
        android:radius="100dp"/>
    <stroke android:color="@color/light_grey"
        android:width="1dp">
    </stroke>
</shape>

然后将其作为背景添加到您的AutocompleteTextview中,如下所示:

<AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:padding="@dimen/padding_10dp"
        android:background="@drawable/searchtextbox_border"
        android:hint="Search" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接