gpt4 book ai didi

java - 以编程方式设置 TextView 的边距

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:51 27 4
gpt4 key购买 nike

我正在尝试创建一个 View ,其中我以编程方式创建的 TextView 之间有静态边距,我怎样才能做到这一点?我尝试了很多,我的

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>

最佳答案

边距在 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扩展和收缩。

关于java - 以编程方式设置 TextView 的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39881932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com