我正在尝试查看以编程方式生成的文本视图之间是否有静态余量的视图,我该如何做到这一点?我尝试了很多

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 - 以编程方式从textviews设置边距,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39881932/

10-10 06:49