params.setMargins无法以编程方式工作

时间:2019-07-16 20:01:19

标签: android

我在xml中设置layout_marginStart和layout_marginTop没关系,但是当我尝试以编程方式设置边距时,它不起作用。我试图找到解决方法,但找不到解决方法。

我不正确的地方是什么?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_resourses">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/MyGridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top|center"
        android:background="@color/borderHigh"
        android:columnCount="2"
        android:orientation="horizontal">

<LinearLayout
            android:id="@+id/firstLinear1"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="@dimen/resourceBorder"
            android:layout_marginTop="@dimen/resourceBorder"
            android:background="@drawable/resource_panel"
            android:orientation="vertical" />
<LinearLayout
            android:id="@+id/firstLinear2"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="@dimen/resourceBorder"
            android:layout_marginTop="@dimen/resourceBorder"
            android:background="@drawable/resource_panel"
            android:orientation="vertical" />
</GridLayout>
</ScrollView>

上述XML有效

我尝试通过代码执行此操作,它必须可以工作,但是我无法解决此问题

Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
int dp9 = dpToPx(9);

GridLayout MyGridLayout = findViewById(R.id.MyGridLayout);
        final int N = 10; // total number of textviews to add
        for (int i = 0; i < N; i++) {
            LinearLayout testlayout = new LinearLayout(this);

            //LinearLayout.LayoutParams tst = new LinearLayout.LayoutParams(0,0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.height = (width/2)-dp9;
            params.width = (width/2)-dp9;
            params.setMargins(6, 6, 0, 0);
            testlayout.setLayoutParams(params);

            testlayout.setOrientation(LinearLayout.VERTICAL);
            testlayout.setBackgroundResource(R.drawable.resource_panel);
            TextView title = new TextView(this);
            title.setText("TextView "+i);
       title.setTextColor(getResources().getColor(R.color.colorPrimary2));
            title.setAllCaps(true);
            title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

         TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(title, 10, 24, 1, TypedValue.COMPLEX_UNIT_SP);
            testlayout.addView(title);

            TextView text = new TextView(this);
            text.setText("Text "+i);
            testlayout.addView(text);

            TextView date = new TextView(this);
            date.setText("date "+i);
            testlayout.addView(date);
        }

0 个答案:

没有答案