【问题标题】:Manage LinearLayout programmatically以编程方式管理 LinearLayout
【发布时间】:2020-04-26 14:43:16
【问题描述】:

我有一个以编程方式构建的类,我需要调整 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】:

    这是我的解决方案:

    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 手动设置每个状态的可绘制对象,例如按下按钮、悬停时等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多