0

以下是向 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));
    }

以下是我的视图 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>

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

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

4

1 回答 1

0

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

于 2016-05-23T18:51:56.070 回答