我试图查看我以编程方式创建的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
&LT;
?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 :(得分: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
扩展和缩小。