以编程方式添加按钮在API级别23中不起作用。我需要动态生成按钮。我该如何添加它们?

时间:2016-05-20 23:43:08

标签: android android-6.0-marshmallow android-api-levels

以下是向LinearLayout动态添加按钮的代码:

private void createAndAddButton()
    {
        LinearLayout top_row =  (LinearLayout)findViewById(R.id.act1toprow);

        // Check if Linear Layout is already populated
        if((top_row).getChildCount() > 0)
            ( top_row).removeAllViews();

        // Create an ArrayList to hold the Button objects that we will create
        final ArrayList<ToggleButton> buttonList = new ArrayList<ToggleButton>();

        final ToggleButton b = new ToggleButton(this);

        b.setGravity(Gravity.CENTER_HORIZONTAL);
        b.setId((int)Math.random());
        b.setTextOff("");
        b.setTextOn("");
        b.setBackgroundDrawable(null);

        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.button0);

        b.setText(null);
        b.setTextOn(null);
        b.setTextOff(null);
        drawable.setBounds(0, 0, (int) (50 * this.getResources().getDisplayMetrics().density), (int) (50 * this.getResources().getDisplayMetrics().density));

        ScaleDrawable sd = new ScaleDrawable(drawable, 0, 100, 100);
        b.setCompoundDrawablesWithIntrinsicBounds(sd.getDrawable(), null, null, null);


        b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                dealWithButtonClick(b);
            }
        });

        buttonList.add(b);

        top_row.addView(buttonList.get(0));
    }

以下是我的View XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ritikasood.testapp.MainActivity">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/togglecard_view"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="190dp"
        card_view:cardCornerRadius="4dp"
        android:backgroundTint="@color/lime"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin">

        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:id="@+id/act1toprow"
                      android:gravity="center"
                      android:backgroundTint="@color/purple"
                      android:orientation="horizontal"
                      android:layout_marginTop="20dp"
                      android:layout_alignParentRight="true"
                      android:layout_alignParentEnd="true"
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentStart="true">
        </LinearLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

没有运行时错误。该应用程序正常启动。

我调试了它运行的代码没有问题,但我仍然没有看到视图上的按钮。

1 个答案:

答案 0 :(得分:0)

ScaleDrawable的初始级别为0,不会绘制任何内容。您需要设置初始级别。