2013-04-22 15 views
5

私は、いくつかのオートコンプリートとテキストボックスを持つ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" 
     /> 
    --> 

+0

に行くことに役立つかもしれwant.Hope任意の形状を描画するのLinearLayoutクラスを実装することでカスタムビューを作成して)(MTDをondraw上書きするrecommandする可能性があります。 //www.gadgetsaint.com/tips/dynamic-square-rectangular-layouts-android/#.WRRjMvl96H s – ASP

答えて

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のソースとして使用しましたが、それ以外の場合は問題ありません。ありがとう! –

+0

LinearLayoutの任意のビュー(ImageView、TextView)にこの背景を追加することができます(この場合、すべてのビューはAndroidに関する限り同じです)。 – Booger

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の使用には意味がありません。代わりにViewを使用すると、LineraLayoutのスタイルに使用する主な属性がすべてあり、レイアウトプロセスの計算の点ではおそらく軽いでしょう。 –

0

私はuが、これは、このHTTPをチェックし、動的な長方形のレイアウトのために右方向

関連する問題