繁体   English   中英

需要帮助以编程方式将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,因为这些是我需要动态添加的。 为简单起见,我只显示一列,但将重复以上七列。

我尝试创建一个单独的类来扩展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而不会感到奇怪:

无法将LinearLayout转换为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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM