将形状添加到LinearLayout Android

问题描述 投票:7回答:3

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

android xml android-layout layout shape
3个回答
11
投票

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

可绘制的形状是用XML创建的,具有以下语法(这个显示一个带圆角的矩形,但是你可以做很多事情来自定义它以便你想要它)。此文件(名为background_square.xml或其他)应放在drawable文件夹中:

 <?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"

2
投票

你可以做下面这样的事情

如果你想添加矩形只需在你的布局中添加嵌套布局(比如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>

您可以根据需要更改所有相关属性,例如大小,边距等


0
投票

我可以建议通过实现Linearlayout类创建自定义视图并覆盖ondraw()mtd以绘制任何你想要的形状。希望这可能有助于朝着正确的方向前进

© www.soinside.com 2019 - 2024. All rights reserved.