【问题标题】:How to use AndroidBootstrap without extends Application如何在没有扩展应用程序的情况下使用 AndroidBootstrap
【发布时间】:2017-09-15 07:30:29
【问题描述】:

我正在尝试使用Android Bootstrap 库。我遵循了快速入门。在快速入门中,它说我应该像这样覆盖我的类:

public class SampleApplication extends Application {
    @Override public void onCreate() {
        super.onCreate();
        TypefaceProvider.registerDefaultIconSets();
    }
}

如何在不扩展 Application 类的情况下使用这个库?我想在我的活动类中使用这个库。

登录活动:

public class Login extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceProvider.registerDefaultIconSets();
        setContentView(R.layout.activity_login);
    }
}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.merve.tev.Login">



    <com.beardedhen.androidbootstrap.BootstrapDropDown
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:bootstrapText="Medium {fa_thumbs_o_up}"
        app:bootstrapBrand="regular"
        app:roundedCorners="true"
        app:bootstrapSize="md"
        app:dropdownResource="@array/bootstrap_dropdown_example_data"
        app:bootstrapExpandDirection="down"
        tools:layout_editor_absoluteY="202dp"
        tools:layout_editor_absoluteX="115dp" />
</LinearLayout>

在我的 MainActivity 类中,我放置了按钮。当我点击它时,我应该去 LoginActivity 类。但是,我收到一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.beardedhen.androidbootstrap.BootstrapDropDown

【问题讨论】:

    标签: android android-bootstrap-widgets


    【解决方案1】:

    在你的活动课上:

    在onCreate()方法中,在setContentView()之前写下这一行;

    protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             TypefaceProvider.registerDefaultIconSets();
             
    }
    

    我希望它会起作用。

    【讨论】:

    • 感谢您的快速回复。我试过你说的。但是现在,我收到一个错误:java.lang.RuntimeException:无法启动活动 ComponentInfo。我将代码添加到我的问题中。
    • 我尝试了您更新的答案,它适用于除下拉列表之外的所有内容。谢谢!
    • 我必须要添加一件事,当我使用这个库时,我在下拉和列表视图控件中也遇到了很多问题..
    【解决方案2】:

    建议在您的应用程序类中调用 TypefaceProvider.registerDefaultIconSets();,因为这会在任何视图显示在屏幕上之前加载 FontAwesome 字体。

    如果您没有加载 FontAwesome 图标,则可以跳过此步骤。如果您担心启动时间,那么您可以尝试异步执行它。

    最后,如果您知道您的应用程序将始终从某个 Activity 启动,那么您可以在调用 setContentView 之前调用 TypefaceProvider.registerDefaultIconSets();,并且应该仍然可以使用 FontAwesome 图标。

    这里唯一的权衡是大多数应用程序都有多个活动作为入口点,这意味着您可能必须将此设置逻辑添加到多个位置。这就是为什么当前的建议是在您的 Application 类中设置它 - 您只需要初始化一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2014-03-03
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多