【问题标题】:get reference to external library xml modules获取对外部库 xml 模块的引用
【发布时间】:2015-11-08 16:10:47
【问题描述】:

我正在使用 navasmdc 的MaterialDesignLibrary

现在我只想获得对 Button 的引用,但我遇到了问题:

不兼容的类型:
必需:com.gc.materialdesign.views.buttonflat 找到:android.view.view

我的代码如下:

   (...)

    ButtonFlat cmdNextButton;

    (...)

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_registration_page_stufe, container, false);

            cmdNextButton = rootView.findViewById(R.id.registration_button_next);

            return rootView;
        }

    (...)

在我的 xml 中只是按钮:

 <com.gc.materialdesign.views.ButtonFlat
                android:id="@+id/registration_button_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimaryDark"
                android:text="weiter" />

我不明白为什么会出现这个问题,因为我在代码中创建的按钮来自我使用的库类。 Android studio 甚至自动导入...

【问题讨论】:

    标签: android xml material-design


    【解决方案1】:

    findViewById() 方法返回一个 View,你需要将它强制转换为它是什么类型的 View。您也需要对常规 Button 执行此操作。

    您可以在this example 中看到分配 ButtonFlat 引用的正确方法。

    只需转换findViewById() 的结果,它应该可以工作:

    cmdNextButton = (ButtonFlat) rootView.findViewById(R.id.registration_button_next);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2013-01-15
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多