2013-10-23 19 views
11

LinearLayoutを含む動的なビューを作成して、それを外側のLinearLayoutに追加します。作成したビューの周囲に余白を設定したいのですが、XMLファイルのlayout_marginは無視されます。コードでパラメータを設定すると動作しますが、レイアウトXMLにマージンを指定したいと思います。 XMLレイアウトにマージンを設定するLinearLayoutで余白が無視される

は無視されます:作成が尊重されながら

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:orientation="vertical" > 

    ... 
</LinearLayout> 

はマージンを設定:

LinearLayout productView = (LinearLayout) getLayoutInflater().inflate(R.layout.product_preview, null); 

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
params.setMargins(50, 50, 50, 50); 
productView.setLayoutParams(params); 

これは外側のレイアウトです。ビューはdealer_activity_product_listに追加されます。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/dealer_activity_dealer_image" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:contentDescription="@string/dealer_activity_dealer_image_desc" /> 

    <TextView 
     android:id="@+id/dealer_activity_dealer_address" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:id="@+id/dealer_activity_product_list1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" /> 

     <LinearLayout 
      android:id="@+id/dealer_activity_product_list2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" /> 
    </LinearLayout> 
</LinearLayout> 

+0

あなたは、階層ビューアを使用してみましたか?またはuiautomatorにビューをダンプしようとしましたか?これらのツールを使用すると、レイアウトを検査することができます。あなたは使用している完全なコードを投稿できますか? – Tobrun

+0

もっとコードを投稿してください。完全なXMLのような?私は間違った商品に余裕を持たせたと思う –

+0

私はHierachyViewerを使ってレイアウトを検査しました。コードのマージンの設定はレイアウトの属性に反映されますが、XMLファイルで設定された値は見つかりません。 – multiholle

答えて

-2

外観図でpadding代わりのlayout_marginを設定します。

は、それが

+1

これは、外側のビューに含まれるすべてのビューの周りにスペースを設定しますが、含まれるビューの間にはスペースを設定しません。 – multiholle

1

あなたはインナーのLinearLayoutまたはのLinearLayoutを含むの属性を設定しているが働くことを願っていますか? は、少なくとも、次はのLinearLayout内の作業を行います。

<TextView 
    android:id="@+id/xxx" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:layout_margin="10dp" 
    /> 
+0

著者は外部レイアウトの余白を設定すると確信しています。 xmlns属性はルート要素にのみ適用可能です –

+0

別の 'LinearLayout'に含まれる' LinearLayout'の属性を設定しています。 – multiholle

-1

共通「ネストされたレイアウト」のパターンがあります。 つまり、補助的なコンテナレイアウトを作成し、コンテナレイアウト内の内部レイアウトを配置する目的の効果を達成します。

並べ替え:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     ... 
     android:layout_margin="20dp" 
     > 
    ... 
    </LinearLayout> 
</LinearLayout> 
+0

これはまさに私がやっていることであり、うまくいかないということです。 – multiholle

+0

外部レイアウトにプログラムで追加する1つのレイアウトか2つのネストされたレイアウトですか? – 18446744073709551615

+0

私は、アンドロイド:layout_marginはXMLのルートであるレイアウトでは無視されると信じていますが、内部レイアウトに対しては 'android:layout_margin'が正常に動作するとは思っていませんでした。この内部レイアウトの親がプログラマチックにその親に追加された場合を除きます)。 – 18446744073709551615

関連する問題