2016-10-05 117 views
1

我試圖製作一個視圖,其中有編程的文本視圖之間存在靜態頁邊距,我如何使這發生?我想了很多,我以編程方式從textviews設置頁邊距

setMargins(viewEvent, 20, placeSum, 0, 0); 

只是設置頁邊距頂部的佈局和說,我想使利潤率以上的TextView。

這裏是我的循環

int set=0; 
     for (int i = 0; i < partEvents.length; i++) { 
      RelativeLayout relativeLayout = (RelativeLayout) eventView.findViewById(R.id.eventRelativeLayout); 
      TextView viewEvent = new TextView(getActivity()); 
      viewEvent.setBackgroundResource(R.drawable.eventbg); 

      viewEvent.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT)); 



      String startHour = getHourFromString(partEvents[i][2]); 
      String startMinute = getMinuteFromString(partEvents[i][2]); 
      String endHour = getHourFromString(partEvents[i][3]); 
      String endMinute = getMinuteFromString(partEvents[i][3]); 

      viewEvent.setText(partEvents[i][0]+ "\n" + 
      partEvents[i][1] + "\n" 
      + startHour + ":" + startMinute + "-" + endHour + ":" + endMinute ); 


      final float scale = getResources().getDisplayMetrics().density; 
      int placeSum = (int) (10 + set * scale + 0.5f); 
      setMargins(viewEvent, 20, placeSum, 0, 0); 


      viewEvent.setTextSize(20); 



      set=set+100; 
      try { 
       relativeLayout.addView(viewEvent); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

XML

<

?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/dividerView" 
    android:layout_alignParentBottom="true"> 

<LinearLayout 
    android:id="@+id/temp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="200dp" 
     android:layout_weight="1" > 

     <TextView 
    android:text="Event namn:" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="12dp" /> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="22dp" 
    android:id="@+id/sum" 
    android:gravity="center_vertical" 
    android:textStyle="normal|bold" 
    android:textSize="20sp" 
    android:layout_alignTop="@+id/textView" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:text="Beskrivning:" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView4" 
    android:layout_below="@+id/sum" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="20dp" /> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="23dp" 
    android:gravity="center_vertical" 
    android:id="@+id/description" 
    android:textStyle="normal|bold" 
    android:textSize="20sp" 
    android:layout_alignTop="@+id/textView4" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:layout_marginTop="21dp" 
    android:id="@+id/startEvent" 
    android:textStyle="normal|bold" 
    android:textSize="20sp" 
    android:layout_alignTop="@+id/textView5" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:text="Tider:" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView5" 
    android:layout_marginTop="14dp" 
    android:layout_below="@+id/description" 
    android:layout_alignParentStart="true" /> 

     <TextView 
      android:text="Time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginEnd="13dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/time" 
      android:textSize="25sp" 
      android:layout_marginLeft="0dp" /> 

    </RelativeLayout> 
</LinearLayout> 
    <View 
     android:id="@+id/dividerView" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_below="@+id/temp" 
     android:background="@color/colorPrimary" /> 


<ScrollView 
    android:id="@+id/eventScrollView" 
    android:layout_width="match_parent" 
    android:overScrollMode="never" 
    android:layout_below="@+id/dividerView" 
    android:padding="0dp" 
    android:scrollbars="none" 
    android:fadingEdge="none" 
    android:layout_height="fill_parent"> 

<RelativeLayout 
    android:id="@+id/eventRelativeLayout" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:minHeight="510dp"> 


    </RelativeLayout> 
</ScrollView> 
+0

你能否提供你的佈局xml文件? – RustWebDev

+0

這裏有什麼提示如何解決它? – egge

+0

邊距是佈局參數。您將它們設置在視圖的佈局參數對象上。 – njzk2

回答

0

保證金在LayoutParams(例如,RelativeLayout.LayoutParams你的情況)作爲

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.setMargins(left, top, right, bottom); 
viewEvent.setLayoutParams(params); 
設置

更新

如果您需要添加TextViewš垂直然後使用LinearLayout作爲

<!-- inside scrollview --> 
<LinearLayout 
    android:id="@+id/eventLinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" /> 

,並以編程爲

LinearLayout linearLayout = (LinearLayout) eventView.findViewById(R.id.eventLinearLayout); 

for (int i = 0; i < partEvents.length; i++) { 

    TextView viewEvent = new TextView(getActivity()); 

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    params.setMargins(left, top, right, bottom); 
    viewEvent.setLayoutParams(params); 

    // rest of your code 

    linearLayout.addView(viewEvent); 
} 

使用LinearLayout,會自動處理您的TextView擴張和收縮。

+0

它解決了問題的一部分,但是如果我在其中一個文本視圖中寫入更長的消息,它將開始隱藏在下面的textview後面。 – egge

+0

我從谷歌日曆api中獲取數據,所以我不想縮短字符串 – egge

相關問題