2013-04-22 72 views
5

我有一個linearLayout有一些自動完成和文本框。我想在linearlayout中插入一個形狀(矩形)。我怎樣才能做到這一點。我是Android新手。將形狀添加到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" 
     /> 
    --> 

+0

去動態的矩形佈局,檢查此http: //www.gadgetsaint.com/tips/dynamic-square-rectangular-layouts-android/#.WRRjMvl96H s – ASP 2017-05-11 13:15:52

回答

9

您應該使用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> 

然後,當你想將其添加到一個視圖中,可以使用下面的語法在XML:

android:background="@drawable/background_square" 
+2

我使用此形狀作爲ImageView的源,但是它很好!謝謝! – 2013-07-11 08:48:35

+0

您可以將此背景添加到LinearLayout的任何視圖 - ImageView,TextView中(在這種情況下,就Android而言,所有視圖都是相同的)。 – Booger 2013-07-11 13:29:55

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

實際上你在正確的道路上,但如果你只是添加一個rectagle,那麼使用LinearLayout是沒有意義的。使用View來代替它,它具有您用來設置LineraLayout的所有主要屬性,並且在佈局過程中它的計算可能更輕 – 2013-04-22 14:38:59

0

我可以通過推薦實施的LinearLayout類來創建自定義視圖和覆蓋的OnDraw()MTD得出任何形狀üwant.Hope這可能有助於在正確的方向