2010-11-23 53 views
100

我編程式地向垂直LinearLayout添加自定義視圖,並且我希望在視圖之間留出一些空間。我嘗試添加:setPadding(0,1,0,1)到我的CustomView構造函數,但這似乎沒有任何效果。有什麼建議?在Android中,如何在LinearLayout子項之間創建空間?

*有人指出我應該使用利潤率。由於我動態添加視圖,因此我需要從代碼(而不是xml)中設置邊距。我相信這樣做的方式在下面,但它不起作用。

public class MyView extends View 
{ 
    public MyView (Context context) 
    { 
     super(context); 

     MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 10, 0, 10); 
     setLayoutParams(params); 

*編輯。我也嘗試使用MarginLayoutParams作爲參數,同時將視圖添加到線性佈局(如下所示)。這也沒有工作:

MarginLayoutParams params = new MarginLayoutParams(linearLayout.getLayoutParams()); 
linearLayout.setMargins(0, 10, 0, 10); 
linearLayout.addView(view, params); 

謝謝。

+0

如果您添加了要添加視圖的XML文件,它可能會有所幫助。 – stpn108 2010-11-23 18:36:46

+0

請參閱我的編輯。 – ab11 2010-11-23 19:01:37

+1

下面是一篇很棒的博客文章:[Cyril Mottier在Android上的Grid Spacing](http://cyrilmottier.com/2014/11/17/grid-spacing-on-android/) – 2016-03-04 12:20:50

回答

86

你應該對android:layout_margin<Side>的孩子。填充是內部的。

+0

您可以用XML定義視圖具有所需的邊距並以程序方式添加預定義的視圖,並將這些內容應用於Java代碼中。 – 2010-11-23 19:04:30

+0

我並不十分清楚你的意思。我應該在xml中定義customview?但是我需要動態創建任意數量的自定義視圖,然後將其添加到我的LinearLayout。 – ab11 2010-11-23 19:14:12

+2

嘗試使用LayoutParams而非MarginLayoutParams。 – Thomas 2010-11-23 19:40:22

4

使用LinearLayout.LayoutParams而不是MarginLayoutParamsHere's的文檔。

21

下面的示例只是以編程方式執行您需要的內容。我已經使用了固定大小(140,398)。

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398); 
     layoutParams.setMargins(24, 0, 24, 0); 
     layout.addView(button,layoutParams); 
26

Android現在支持在視圖之間添加Space視圖。它從4.0 ICS開始提供。

135

API> = 11溶液:

可以填充集成到分頻器。如果你正在使用無,只需要創建一個高大空的繪製,並設置爲LinearLayout的除法:

<LinearLayout 
      android:showDividers="middle" 
      android:divider="@drawable/empty_tall_divider" 
...>...</LinearLayout> 

empty_tall_divider.xml:

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <size 
      android:height="40dp" 
      android:width="0dp"/> 
</shape> 
3

如果使用ActionBarSherlock,您可以使用COM。 actionbarsherlock.internal.widget.IcsLinearLayout:

<com.actionbarsherlock.internal.widget.IcsLinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:divider="@drawable/list_view_divider" 
     android:dividerPadding="2dp" 
     android:showDividers="middle" > 
... 
</com.actionbarsherlock.internal.widget.IcsLinearLayout> 
2

由於API等級14,你可以只添加(透明)分隔繪製:

android:divider="@drawable/divider" 
android:showDividers="middle" 

它會爲您處理剩下的問題!

1

在子視圖的佈局中使用填充。

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="@drawable/backage_text" 
      android:textColor="#999999" 
      > 

</TextView> 

backage_text。XML

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

    <solid android:color="@color/white"/> 
    <corners android:radius="2dp"/> 
    <stroke 
     android:width="1dp" 
     android:color="#999999"/> 
    <padding 
     android:bottom="5dp" 
     android:left="10dp" 
     android:right="10dp" 
     android:top="5dp" /> 
</shape> 
1

你可以得到LayoutParamsLinearLayout的,適用於個人的看法是這樣的:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
lp.setMargins(8,8,8,8); 
  • 請小心,setMargins()取像素爲int數據type.So,轉換在添加值之前添加到dp
  • 上面的代碼將高度和寬度設置爲wrap_content。你可以自定義它。
相關問題