以编程方式管理 LinearLayout
Manage LinearLayout programmatically
我有一个正在以编程方式构建的 class,我需要调整 2 按钮布局,使其看起来像我拥有的其他 class。
在这个 class AttributeWizard 中,我通过代码进行操作,因为我进行数据库操作并且需要像这样构建 GUI。
我想调整按钮 (layoutButtons) 的布局以适应屏幕的整个宽度。目前他们没有填满整个 space.
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>
我什么时候应该更改属性?添加到主布局后?前?我设置对了吗属性?
这是我的解决方案:
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;
}
}
创建新的 Views/ViewGroups 时,您通常会创建新的 LayoutParams,设置布局属性,然后调用 setLayoutParams()
。
更改已添加的Views/ViewGroups的布局属性时,您可以获取其LayoutParams,设置属性,然后调用setLayoutParams()
。例如:
LinearLayout.LayoutParams params = layoutBotoes.getLayoutParams();
params.weight = 0;
layoutBotoes.setLayoutParams(params);
When should I change properties? After adding to the main layout? Before?
没关系,只要你打电话setLayoutParams()
。您需要始终调用 setLayoutParams()
以使布局属性的更改生效。
I want to adjust the layout of the buttons (layoutButtons) to fit the full width of the screen. Currently they do not fill the entire space.
您需要将每个按钮的权重设置为 1。根据其 javadoc 描述,权重:
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
为每个按钮赋予 1 的权重,允许它们以相等的比例填充额外的 space。
I want the buttons to be at the bottom
首先给layoutBotoes
一个权重1来填充额外的space。然后,将每个按钮的 layout_gravity
设置为 BOTTOM
。要以编程方式执行此操作:
params.gravity = Gravity.BOTTOM;
if anyone knows how to remove the tab between them, thank you
用于按钮背景的图像有额外的填充。您可以将其背景更改为另一个没有的可绘制对象。您需要使用 StateList Drawable 为每个状态手动设置可绘制对象,例如按下按钮、悬停等。
我有一个正在以编程方式构建的 class,我需要调整 2 按钮布局,使其看起来像我拥有的其他 class。
在这个 class AttributeWizard 中,我通过代码进行操作,因为我进行数据库操作并且需要像这样构建 GUI。
我想调整按钮 (layoutButtons) 的布局以适应屏幕的整个宽度。目前他们没有填满整个 space.
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>
我什么时候应该更改属性?添加到主布局后?前?我设置对了吗属性?
这是我的解决方案:
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;
}
}
创建新的 Views/ViewGroups 时,您通常会创建新的 LayoutParams,设置布局属性,然后调用 setLayoutParams()
。
更改已添加的Views/ViewGroups的布局属性时,您可以获取其LayoutParams,设置属性,然后调用setLayoutParams()
。例如:
LinearLayout.LayoutParams params = layoutBotoes.getLayoutParams();
params.weight = 0;
layoutBotoes.setLayoutParams(params);
When should I change properties? After adding to the main layout? Before?
没关系,只要你打电话setLayoutParams()
。您需要始终调用 setLayoutParams()
以使布局属性的更改生效。
I want to adjust the layout of the buttons (layoutButtons) to fit the full width of the screen. Currently they do not fill the entire space.
您需要将每个按钮的权重设置为 1。根据其 javadoc 描述,权重:
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
为每个按钮赋予 1 的权重,允许它们以相等的比例填充额外的 space。
I want the buttons to be at the bottom
首先给layoutBotoes
一个权重1来填充额外的space。然后,将每个按钮的 layout_gravity
设置为 BOTTOM
。要以编程方式执行此操作:
params.gravity = Gravity.BOTTOM;
if anyone knows how to remove the tab between them, thank you
用于按钮背景的图像有额外的填充。您可以将其背景更改为另一个没有的可绘制对象。您需要使用 StateList Drawable 为每个状态手动设置可绘制对象,例如按下按钮、悬停等。