【问题标题】:(Android Studio) Why are buttons added through XML different than those added through java. How can i make them constant [duplicate](Android Studio) 为什么通过 XML 添加的按钮与通过 java 添加的按钮不同。我怎样才能使它们保持不变[重复]
【发布时间】: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 文件吗?

标签: java android


【解决方案1】:

如果您希望它们具有相同的样式,则可以使用LayoutInflater.from(this).inflate(R.layout.button_layout_id)
而不是new Button(this),您需要将按钮提取到一个独立的xml布局文件中

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2014-05-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多