2017-02-24 107 views
2

我做充气布局。我想给布局中的每个项目之间的间距。像下面在充气布局中的每个项目布局之间添加空间android

Left side what i want to do, the right what i do

这是我在XML主代码的图像。父布局

<LinearLayout 
      android:id="@+id/mLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:background="#F2F2F2" 
      android:orientation="vertical" 
      android:padding="2dp"/> 

这是我的充气布局

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:padding="3dp" 
android:layout_marginTop="5dp" 
android:background="@color/white_pure"> 


<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="5" 
    android:orientation="vertical"> 

    <TextView 
     android:textStyle="bold" 
     android:textSize="12sp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:textSize="9sp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp"/> 


</LinearLayout> 


<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="3" 
    android:gravity="right" 
    android:layout_marginRight="5dp" 
    android:textColor="@color/blueTransfer"/> 

    </LinearLayout> 

我如何做才能像图像(左侧)。在充气布局中的每个项目之间有间距。希望任何人都可以帮助我。

+1

使用余量而不是填充。然后,为每个部分设置背景白色和灰色。 – TruongHieu

+0

如何?我想我已经为每个布局设置了保证金。 huhuu –

+0

你能否提供完整的每个项目视图xml? – TruongHieu

回答

2

一个soluion是改变你的项目布局类似这样的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="3dp" 
    > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:background="#fff" 
     > 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="5" 
      android:orientation="vertical" 
      > 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:text="aaaaaaaaaaaa" 
       android:textColor="#000" 
       android:textSize="12sp" 
       android:textStyle="bold" 
       /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:text="bbbbbbbbbb" 
       android:textColor="#000" 
       android:textSize="9sp" 
       /> 
     </LinearLayout> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginRight="5dp" 
      android:layout_weight="3" 
      android:gravity="right|center" 
      android:text="Details" 
      android:textColor="#00f" 
      /> 
    </LinearLayout> 

</LinearLayout> 

enter image description here

另一种解决方案是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginStart="5dp" 
    android:layout_marginEnd="5dp" 
    android:background="#fff" 
    android:orientation="horizontal" 
    android:padding="3dp" 
    > 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:orientation="vertical" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:text="aaaaaaaaaaaa" 
      android:textColor="#000" 
      android:textSize="12sp" 
      android:textStyle="bold" 
      /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:text="bbbbbbbbbb" 
      android:textColor="#000" 
      android:textSize="9sp" 
      /> 
    </LinearLayout> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginRight="5dp" 
     android:layout_weight="3" 
     android:gravity="right|center" 
     android:text="Details" 
     android:textColor="#00f" 
     /> 
</LinearLayout> 

这时如果使用ListView,你可以增加ListView项目之间的间距通过使用android:dividerhttps://stackoverflow.com/a/5309871/5381331

或为RecyclerViewHow to add dividers and spaces between items in RecyclerView?

+0

感谢它的工作..我也只是认为添加视图作为技巧 –

+0

@ user7345006你也可以检查解决方案2;) –

+0

但这一个android:gravity =“right | center”我不能使用..得到错误..如何让词汇在中心和正确的位置? –

0

试试这个,

<?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="wrap_content" 
     android:layout_marginTop="5dp" 
     android:background="@color/white_pure" 
     android:orientation="horizontal" 
     android:padding="3dp"> 


     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_toStartOf="@+id/tv_details" 
      android:orientation="vertical" 
      android:padding="10dp"> 

      <TextView 
       android:id="@+id/tv_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="12sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tv_title" 
       android:layout_marginTop="5dp" 
       android:textSize="9sp" /> 


     </RelativeLayout> 


     <TextView 
      android:id="@+id/tv_details" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="5dp" 
      android:layout_weight="3" 
      android:gravity="right" 
      android:padding="10dp" 
      android:layout_centerVertical="true" 
      android:textColor="@color/blueTransfer" /> 

    </RelativeLayout> 
0

您还可以设置在Java代码的两个充气布局之间的空间(保证金)以下一样,这对我的作品。

View v =LayoutInflater.from(this).inflate(R.layout.item,null); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
layoutParams.setMargins(0,10,0,0); 
v.setLayoutParams(layoutParams); 

希望这将有助于你

0
<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_marginTop="5dp" 
android:background="@color/white_pure"> 

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

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:orientation="vertical"> 

     <TextView 
      android:textStyle="bold" 
      android:textSize="12sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Order Attempt 1" 
      android:layout_marginLeft="5dp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:textSize="9sp" 
      android:layout_height="wrap_content" 
      android:text="aa" 
      android:layout_marginLeft="5dp"/> 


    </LinearLayout> 


    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:gravity="right" 
     android:layout_marginRight="5dp" 
     android:text="DETAILS" 
     android:textColor="@color/blueTransfer"/> 
</LinearLayout> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="3dp" 
    android:background="#F2F2F2" /> 

我也只是觉得这一招..把图..它也工作了..谢谢大家帮我..: - )

0

只是使用pading,而不是在项目的顶部group.and列表视图使用margingLeft和margingRight.item代码这样。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:paddingTop="3dp" 
android:paddingBottom="3dp" 
android:background="@color/white_pure">