Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将形状添加到LinearLayout Android_Android_Xml_Android Layout_Layout_Shape - Fatal编程技术网

将形状添加到LinearLayout Android

将形状添加到LinearLayout Android,android,xml,android-layout,layout,shape,Android,Xml,Android Layout,Layout,Shape,我有一个线性布局,有一些自动完成和文本框。我想在linearlayout中插入一个形状(矩形)。我怎样才能做到这一点。我是android的新手 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_wi

我有一个线性布局,有一些自动完成和文本框。我想在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"
       />
   -->


您可以执行以下操作

如果要添加矩形,只需在布局中添加嵌套布局(例如linearlayout) 并设置
android:background=“yourcolor”
//您可以使用哈希颜色值添加颜色

<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"
   />
//suppose you want to add your rectangle here
<LinearLayout 
android:id="@+id/rectangle"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:background="yourcolor"
>
</LinearLayout>

//假设要在此处添加矩形

您可以根据需要更改所有相关属性,如大小、边距等。您应该使用ShapedRavable作为要添加样式的组件的背景

一个可绘制的形状是用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>

我可能会建议通过实现Linearlayout类来创建自定义视图,并重写ondraw()mtd来绘制您想要的任何形状。希望这可以帮助您朝着正确的方向前进

实际上,您的路径是正确的,但是如果您只想添加一个矩形,那么使用Linearlayout就没有意义了。改为使用视图,它具有用于设置LinerLayout样式的所有主要属性,而且在布局过程中,它的计算量可能更轻。我将此形状用作ImageView的源,但在其他方面,它也很好!谢谢您可以将此背景添加到LinearLayout的任何视图—ImageView、TextView(在本例中,就Android而言,所有视图都是相同的)。
android:background="@drawable/background_square"