9.20

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp"

    android:gravity="center">

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TextView 1"

        android:textSize="18sp"

        android:layout_marginBottom="8dp" />

 

    <TextView

        android:id="@+id/textView2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="TextView 2 with weight"

        android:textSize="18sp"

        android:layout_weight="1" />

 

</LinearLayout>

 

 

 

 

 

 

3.9

所花时间(包括上课):1.5

代码(行):150

博客量(篇):1

了解到知识点:设置视图的宽高和间距

<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-20 20:37  赵千万  阅读(8)  评论(0)    收藏  举报