9.21

<Button

    android:layout_width="100dp"

    android:layout_height="100dp"

    android:text="Button" />

 

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical">

 

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Button 1"

        android:layout_marginBottom="16dp" />

 

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Button 2" />

 

</LinearLayout>

// 获取视图对象

Button button = findViewById(R.id.button);

 

// 设置宽度和高度

LayoutParams layoutParams = button.getLayoutParams();

layoutParams.width = 200; // 设置宽度为200像素

layoutParams.height = LayoutParams.WRAP_CONTENT; // 设置高度为包裹内容

button.setLayoutParams(layoutParams);

 

// 设置间距

Int marginInPixels = getResources().getDimensionPixelSize(R.dimen.margin_size); // 从资源文件中获取间距值

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();

params.setMargins(marginInPixels, marginInPixels, marginInPixels, marginInPixels); // 设置上下左右间距

button.setLayoutParams(params);

posted @ 2024-09-21 19:35  赵千万  阅读(5)  评论(0)    收藏  举报