需要帮助以编程方式将 ToggleButton 添加到具有一半 layout_weight 的 FrameLayout

Need Help adding programmatically adding ToggleButton to a FrameLayout with half the layout_weight

我的目标是动态地将一个 toggleButton 添加到 frameLayout(它有自己的权重),但按钮的宽度是 frameLayout 的一半。我可以在 XML 中创建我想要的内容,但我在尝试以编程方式完成这项工作时遇到了问题。

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/columns"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="8">

        <FrameLayout
            android:id="@+id/column_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@android:color/background_dark" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/buttonLayoutWrapper"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="2">

                <ToggleButton
                    android:layout_width="0dp"
                    android:layout_height="360dp"
                    android:layout_weight="1"
                    android:checked="true"
                    android:text=""
                    android:textOff=""
                    android:textOn=""/>

            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

</LinearLayout>

请特别注意 ID 为 buttonLayoutWrapper 的 LinearLayout 及其子 ToggleButton,因为它们是我需要动态添加的。为简单起见,我只显示一列,但会再重复 7 列。

我尝试创建一个单独的 class 来扩展 LinearLayout 并添加 ToggleButton 认为我可以通过这种方式获得重量,但没有成功。

无论我做什么,我似乎都无法让 ToggleButton 服从 layout_weight。

下面是尝试以编程方式执行此操作的示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1);
    ToggleButton toggleButton = new ToggleButton(this);
    LinearLayout.LayoutParams layoutWrapper = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 360, .5f);
    toggleButton.setChecked(true);
    toggleButton.setText("");
    toggleButton.setTextOn("");
    toggleButton.setTextOff("");
    toggleButton.setLayoutParams(layoutWrapper);
    frameLayout.addView(toggleButton);
}

似乎我需要一个父 LinearLayout 来设置 weightSum 以使布局权重在 XML 版本中工作。但是,我似乎无法弄清楚如何向其中添加一个 LinearLayout 父级,然后可以将其添加到 FrameLayout 而不会出现奇怪的情况:

cannot cast LinearLayout to FrameLayout

编译时出错。

重写

尝试以下操作。它将 ToggleButton 添加到 LinearLayout,然后将 LinearLayout 添加到 FrameLayout。我想这就是你要找的。 (注释掉 XML 中的 LinearLayoutToggleButton 以便您可以看到此作品。)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final float dpToPx = getResources().getDisplayMetrics().density;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout linearLayout = new LinearLayout(this);
        final FrameLayout.LayoutParams frameLP =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                                         FrameLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(frameLP);
        linearLayout.setId(View.generateViewId()); // API 17+ needed
        linearLayout.setWeightSum(2.0f);

        final ToggleButton toggleButton = new ToggleButton(this);
        final LinearLayout.LayoutParams linearLP =
            new LinearLayout.LayoutParams(0, (int) (360 * dpToPx), 1.0f);
        toggleButton.setLayoutParams(linearLP);
        toggleButton.setChecked(true);
        toggleButton.setText("");
        toggleButton.setTextOn("");
        toggleButton.setTextOff("");

        linearLayout.addView(toggleButton);
        final FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1);
        frameLayout.addView(linearLayout);
    }
}