【问题标题】:Add abstract View to layout and decide the specific class programatically将抽象视图添加到布局并以编程方式确定特定类
【发布时间】:2017-04-07 07:11:46
【问题描述】:

我有一个带有片段查看器的应用程序。

每个片段要么属于 A 类型,包含 5 个 CheckBoxes 的列表,要么属于 B 类型,包含 5 个 Radiobuttons

目前,我有一个 AbstractFragment,它有 2 个子类 FragmentAFragmentB,我设置了一个带有列表或其他的布局。

我想简化这一点,只有一个 Fragment 类,列表中有 5 个 CompoundButton,如果 CompoundButtonCheckBoxRadiobutton,则以编程方式设置。

由于CompoundButton 是抽象的,我怎样才能实现这样的目标?

PS:当然,以编程方式添加按钮不是我想做的,因为布局有点复杂,设置起来很乏味。

【问题讨论】:

    标签: android android-layout checkbox radio-button


    【解决方案1】:
    public class ComponentFragment extends Fragment {
        private ComponentFragmentMode mode = ComponentFragmentMode.CHECKBOX;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            if (mode == ComponentFragmentMode.CHECKBOX) {
                return inflater.inflate(R.layout.checkbox_layout, container, false);
            } else {
                return inflater.inflate(R.layout.radiobutton_layout, container, false);
            }
    }
    

    【讨论】:

    • 我想要一个单一的布局并决定一些小部件的类。但是您的解决方案对我来说仍然是一个重大改进,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多