将形状添加到LinearLayout

将形状添加到LinearLayout

本文介绍了将形状添加到LinearLayout Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些自动完成和文本框的linearLayout.我想在linearlayout中插入一个形状(矩形).我怎么能做到这一点.我是android的新手.

I have a linearLayout having some autocomplete and textboxes. I want to insert a shape (rectangle) in linearlayout. How can i achieve this. I am new comer to 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用于要将样式添加到的组件的背景.

You should use a ShapeDrawable for the background of the component you want to add the style to.

使用以下语法在XML中创建可绘制形状(此形状显示带有圆角的矩形,但是可以根据需要自定义外观很多).此文件(名为background_square.xml或其他名称)应放在您的可绘制文件夹中:

A shape drawable is created in XML, with the following syntax (this one shows a rectangle, with the rounded corners, but there is lots you can do to customize this to look however you want). This file (named background_square.xml or whatever) should be put in your drawable folder:

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

然后,当您要将其添加到视图时,可以在XML中使用以下语法:

Then, when you want to add it to a View, you can use the following syntax in your XML:

android:background="@drawable/background_square"

这篇关于将形状添加到LinearLayout Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:29