2016-11-15 13 views
1

RelativeLayoutViewを追加して300dpの余白を残しました もう1つのビューを追加しましたが、2番目のビューは最初の余白の影響を受けません。どうして ?相対レイアウトとマージン?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:layout_marginLeft="300dp" 
     android:background="@color/colorPrimaryDark" 
     android:text="use margin left view 1" 
     android:textSize="40sp" /> 

    <TextView 
     android:layout_width="290dp" 
     android:layout_height="200dp" 
     android:background="@color/colorPrimaryDark" 
     android:text="not affected by margin view 2 " 
     android:textSize="40sp" /> 
</RelativeLayout> 

結果:

enter image description here

答えて

0

あなたは互いに対する子ビューを固定する必要のあるRelativeLayoutに入れているからです。

この場合、android:layout_toRightOf=´="@+id/text"という属性を2番目のTextViewに追加する必要があります。

全体のレイアウトは、次のようになります。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:text="Some text" 
     android:layout_marginLeft="300dp" 
     android:textSize="40sp" 
     android:background="@color/colorPrimaryDark"/> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_toRightOf=="@+id/text1" 
     android:layout_width="290dp" 
     android:layout_height="200dp" 
     android:text="Some other text" 
     android:textSize="40sp" 
     android:background="@color/colorPrimaryDark"/> 

</RelativeLayout> 

あなたが最初TextViewではなく、それらの両方にmarginLeftを適用しているため、この問題が発生https://developer.android.com/guide/topics/ui/layout/relative.html

0

ここRelativeLayoutについての詳細を読むことができます。あなたは両方のTextViewsmarginLeftを適用したい場合は、LinearLayoutでそれらの両方を包囲し、その後LinearLayout 300dpのmarginLeftを与えるか、あなたはここで

300dpコードは両方をラップすることです両方TextViewsmarginLeftで与えることができる可能性がありLinearLayoutTextViews

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     //Other stuff here 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="300dp"> 

     <TextView 
       android:layout_width="200dp" 
       android:layout_height="200dp" 
       android:text="use margin left view 1" 
       android:id="@+id/text" 
       android:textSize="40sp" 
       android:background="@color/colorPrimaryDark"/> 

     <TextView 
       android:layout_width="290dp" 
       android:layout_height="200dp" 
       android:text="not affected by margin view 2 " 
       android:textSize="40sp" 
       android:background="@color/colorPrimaryDark"/> 
    </LinearLayout> 

    //Other stuff here 
</RelativeLayout> 

また、ちょうどそのようなalignLeftと同じ望ましい結果を得るためにRelativeLayoutのプロパティを使用することができます。

試して属性 RelativeLayoutその他
<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    <TextView 
      android:id="@+id/text" 
      android:layout_width="200dp" 
      android:layout_height="200dp" 
      android:layout_marginLeft="300dp" 
      android:text="use margin left view 1" 
      android:textSize="40sp" 
      android:background="@color/colorPrimaryDark"/> 

    <TextView 
      android:layout_width="290dp" 
      android:layout_height="200dp" 
      android:layout_alignLeft="@id/text" 
      android:text="not affected by margin view 2 " 
      android:textSize="40sp" 
      android:background="@color/colorPrimaryDark"/> 

</RelativeLayout> 

がここで見つけることができます:https://developer.android.com/reference/android/widget/RelativeLayout.html

、ここではいくつかが使用されての例です: https://developer.android.com/guide/topics/ui/layout/relative.html

0

RelativeLayoutここ

コードですコンテナです。ある子供から別の子供に自動的にレイアウトパラメータを渡すことはありません。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_marginLeft="300dp" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="200dp" 
     android:layout_height="200p" 
     android:text="text 1" 
     android:background="@color/colorPrimaryDark"/> 

    <TextView 
     android:layout_width="290dp" 
     android:layout_height="200dp" 
     android:text="text 2" 
     android:background="@color/colorPrimaryDark"/> 

</RelativeLayout> 
0

にです:RelativeLayoutにすべての子供を作るために

は同じ左マージンを持って、その後私はあなたのコードは次のようになり代わりTextView

RelativeLayout自体にマージンを追加することをお勧めこの場合、LinearLayoutを使用する方がよいでしょう。オリエンテーションについて忘れないでください:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_marginLeft="300dp" 
    android:background="@color/colorPrimaryDark" 
    android:text="use margin left view 1" 
    android:textSize="40sp" /> 

<TextView 
    android:layout_width="290dp" 
    android:layout_height="200dp" 
    android:background="@color/colorPrimaryDark" 
    android:text="affected by margin view 2 " 
    android:textSize="40sp" /> 

</LinearLayout> 
関連する問題