Adding Shape to LinearLayout Android

2019-02-05 05:02发布

问题:

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

回答1:

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

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>

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

android:background="@drawable/background_square"


回答2:

You can do something like below

if you want to add rectangle simply add nested layout(say linearlayout) within your layout and set android:background="yourcolor" //you can add color using hash color values

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

you change all the relative properties as you want say size,margins,etc



回答3:

I May recommand to create custom view by implementing Linearlayout class and override ondraw() mtd to draw any shape u want.Hope this may help in to go in right direction