【发布时间】:2021-04-14 00:18:56
【问题描述】:
当我使用 XML 添加按钮时,与通过 Java 添加按钮相比,它使用不同的样式。我已经研究了 android studio 中的样式和主题是如何工作的,但我似乎无法弄清楚。直到紫色删除按钮的所有内容都是 XML。我想用 Java 制作一个副本,以便轻松复制。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollview_Notifications"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".NotificationsActivity">
<LinearLayout
android:id="@+id/linear_layout_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button_plot_route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/def_margin"
android:layout_gravity="center_horizontal"
android:text="@string/plot_route"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<!-- Column left to right -->
<!-- Row up and down -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/def_margin"
android:layout_weight="1"
android:text="222222222222222"
android:layout_gravity=""/>
<TableRow>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/def_margin"
android:layout_weight="3"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/def_margin"
android:text="@string/enable"
android:layout_weight="1"/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/def_margin"
android:text="@string/delete"
android:layout_weight="1"/>
</TableLayout>
</LinearLayout>
</ScrollView>
// Make table
TableLayout newTable = new TableLayout(this);
// Add title
TextView title = new TextView(this);
title.setText(name);
title.setGravity(Gravity.CENTER_HORIZONTAL);
newTable.addView(title);
// Params
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow newRow = new TableRow(this);
// Date
EditText newEditText = new EditText(this);
newEditText.setText(date);
trParams.weight = 3;
newRow.addView(newEditText,trParams);
// Enable
trParams.weight =1;
CheckBox checkBox = new CheckBox(this);
checkBox.setText("Enable");
newRow.addView(checkBox, trParams);
newTable.addView(newRow);
Button del = new Button(this);
del.setText("Delete");
del.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
}
});
newTable.addView(del);
llMain.addView(newTable);
llMain.invalidate();
【问题讨论】:
-
哪个按钮是代码添加的,哪个是XML添加的?能给我们看看代码和 XML 文件吗?