android - 将形状添加到 LinearLayout Android

标签 android xml android-layout layout shapes

我有一个带有一些自动完成和文本框的 linearLayout。我想在线性布局中插入一个形状(矩形)。我怎样才能做到这一点。我是 android 的新手。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:padding="5dp"
    android:id="@+id/layout">

    <AutoCompleteTextView android:id="@+id/autocompleteCountry"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/CONTRY_LABEL"
       />
    <AutoCompleteTextView android:id="@+id/locationAutoCompleteFrom"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/from"
        android:visibility="gone"
       />
   <AutoCompleteTextView android:id="@+id/locationAutoCompleteTO"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to"
        android:visibility="gone"
       />
 <!--    <Button android:id="@+id/buttonRoute"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/buttonRouteText"
       android:enabled="false"
       android:clickable="true"
       />
   -->

最佳答案

您应该为要添加样式的组件的背景使用 ShapeDrawable。

形状可绘制对象是在 XML 中创建的,使用以下语法(这个显示了一个矩形,带有圆角,但是您可以做很多事情来自定义它以使其看起来像您想要的那样)。此文件(名为 background_square.xml 或其他名称)应放在您的可绘制文件夹中:

 <?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <solid
        android:color="@color/primary_grey" /> 
</shape>

然后,当您想将它添加到 View 时,可以在 XML 中使用以下语法:

android:background="@drawable/background_square"

关于android - 将形状添加到 LinearLayout Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16149475/

相关文章:

java - 如何处理被拒绝的权限 Android M (EasyPermissions)

android - 使用 Admob 的 ConfigChanges 在 AndroidManifest.xml 中声明的 AdActivity

android - 如何创建用于测试的对象的 PagedList?

Java 解析带有未声明命名空间的 xml

android - CardView 在移除高程后占用高程空间

android visibility - ImageView 的预留空间

java - RxJava : throw IllegalArgumentException when use 'first' operator

JDK中XML相关api

jquery - 使用 jQuery 解析 XML 的奇怪错误

android - 添加 RadioButton 后,ListView onClickListener() 不起作用