以编程方式管理LinearLayout

问题描述 投票:0回答:1

我有一个正在以编程方式构建的类,我需要调整2按钮的布局以使其看起来像我所拥有的其他类。

在此类AttributeWizard中,我正在通过代码进行操作,因为我进行数据库操作并且需要构建这样的GUI。

我想调整按钮(layoutButtons)的布局以适合屏幕的整个宽度。目前,它们无法填充整个空间。

public class AttributeWizard extends Activity {

private LinearLayout linearLayoutAttribute;
private LinearLayout layoutBotoes;
private Button bNext;
private Button bPrev;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_uisarde_attributes);
    linearLayoutAttribute = findViewById(R.id.linearLayoutAttribute);

    createLayoutForButtons();

    linearLayoutAttribute.addView(layoutBotoes);

    layoutBotoes.getLayoutParams().height = LinearLayout.LayoutParams.FILL_PARENT;
    layoutBotoes.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
}


public void createLayoutForButtons() {
    layoutBotoes = new LinearLayout(AttributeWizard.this);
    bNext = new Button(AttributeWizard.this);
    bNext.setText("Próximo");
    bPrev = new Button(AttributeWizard.this);
    bPrev.setText("Anterior");

    layoutBotoes.setOrientation(LinearLayout.HORIZONTAL);
    layoutBotoes.addView(bPrev);
    layoutBotoes.addView(bNext);
}

}

[activity_uisarde_percurso.xml布局文件以示例为例,说明我希望按钮位于底部(如果有人知道如何删除它们之间的选项卡,谢谢)]

<?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="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        style="?android:textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="Seleção de percurso"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvInfoProperty"
        style="?android:textAppearanceSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/inst_percurso" />

    <RadioGroup
        android:id="@+id/rgPercurso"
        style="?android:textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp" >

        <RadioButton
            android:id="@+id/rbPadrao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Padrão" />

        <RadioButton
            android:id="@+id/rbPersonalizado"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Personalizado" />
    </RadioGroup>

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:orientation="horizontal">

        <Button
            android:id="@+id/prev_button_percurso"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selectable_item_background"
            android:text="@string/prev" />

        <Button
            android:id="@+id/next_button_percurso"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selectable_item_background"
            android:text="@string/next" />

    </LinearLayout>

</LinearLayout>

我应该何时更改属性?添加到主布局后?之前?我要设置正确的属性吗?

java android xml android-linearlayout
1个回答
0
投票

这是我的解决方案:

public class AttributeWizard extends Activity {

    private LinearLayout linearLayoutAttribute;
    private LinearLayout layoutBotoes;
    private Button bNext;
    private Button bPrev;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_uisarde_attributes);

        createLayoutForButtons();

        linearLayoutAttribute = findViewById(R.id.linearLayoutAttribute);
        linearLayoutAttribute.addView(layoutBotoes);
    }


    public void createLayoutForButtons() {
        layoutBotoes = new LinearLayout(this);
        bNext = createButton();
        bNext.setText("Próximo");
        bPrev = createButton();
        bPrev.setText("Anterior");

        layoutBotoes.setOrientation(LinearLayout.HORIZONTAL);
        layoutBotoes.addView(bPrev);
        layoutBotoes.addView(bNext);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, 0);
        params.weight = 1;
        layoutBotoes.setLayoutParams(params);
    }

    private Button createButton() {
        Button button = new Button(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, WRAP_CONTENT);
        params.weight = 1;
        params.gravity = Gravity.BOTTOM;
        button.setLayoutParams(params);
        return button;
    }
}

[创建新的视图/视图组时,通常会创建新的LayoutParams,设置布局属性,然后调用setLayoutParams()

[更改已添加的Views / ViewGroups的布局属性时,可以获取其LayoutParams,设置属性,然后调用setLayoutParams()。例如:

LinearLayout.LayoutParams params = layoutBotoes.getLayoutParams();
params.weight = 0;
layoutBotoes.setLayoutParams(params);

我应该何时更改属性?添加到主布局后?以前?

无所谓,只要您叫setLayoutParams()。您需要始终调用setLayoutParams()才能使布局属性的更改生效。

我想调整按钮(layoutButtons)的布局以适合屏幕的整个宽度。目前,它们无法填充整个空间。

您需要将每个按钮的权重设置为1。根据其javadoc描述,权重:

表示将LinearLayout中的多余空间分配给与这些LayoutParams关联的视图。如果不应拉伸视图,则指定0。否则,多余像素将在权重大于0的所有视图中按比例分配。

将每个按钮的权重设置为1,可使两个按钮均等地填充额外的空间。

我希望按钮位于底部

首先给layoutBotoes赋予1的权重以填充额外的空间。然后,将每个按钮的layout_gravity设置为BOTTOM。以编程方式执行此操作:

params.gravity = Gravity.BOTTOM;

[如果有人知道如何删除他们之间的标签,谢谢]

用于按钮背景的图像具有额外的填充。您可以将其背景更改为另一个没有对象的可绘制对象。您需要使用StateList Drawable来手动设置每种状态的可绘制状态,例如按下,悬停等。

© www.soinside.com 2019 - 2024. All rights reserved.