如何创建浮动按钮

时间:2016-11-29 11:04:22

标签: android button floating-action-button

我正在尝试学习如何创建一个浮动按钮,所以我参考了这个教程http://www.androidhive.info/2015/12/android-material-design-floating-action-button/,但问题是当我运行App时,我收到以下内容 发布了logcat错误,第26行引用了这一行

请看看下面发布的文件和我收到的错误信息,请让我知道如何解决项目

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

floating_button

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.bestandlayout_00.ActMain">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/act_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"
    app:backgroundTint="@color/colorAccent"/>

</android.support.design.widget.CoordinatorLayout>

actMain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bestandlayout_00.ActMain">

<include
    layout="@layout/status"></include>

<include
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout="@layout/meine_vertaege"></include>

gradle app

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
    applicationId "com.example.bestandlayout_00"
    minSdkVersion 21
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:24.1.1'

}

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">100dp</item>
    <item name="android:background">?android:attr/listDivider</item>
    <item name="colorPrimary">@color/colorPrimary</item>
</style>

</resources>

logcat错误

FATAL EXCEPTION: main
Process: com.example.bestandlayout_00, PID: 26153
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bestandlayout_00/com.example.bestandlayout_00.ActMain}: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.FloatingActionButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.bestandlayout_00.ActMain.onCreate(ActMain.java:25)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7331) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

2 个答案:

答案 0 :(得分:0)

com.android.support:appcompat-v7:21+增加了对运行pre android 5.1(API Level 21)的设备上的调色小部件的支持。要使用它,请确保扩展或设置AppCompat主题并使用app:backgroundTint而不是android:backgroundTint。

实施例:

的xmlns:应用= “http://schemas.android.com/apk/res-auto”     机器人:ID = “@ + ID / FAB”

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="ars" password="1234" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

如果您使用Activity,请将其更改为AppCompatActivity。当它是Activity

时可能会出现错误

答案 1 :(得分:0)

将此添加到gradle依赖项

编译'com.android.support:design:25.0.0'

dependencies {
compile 'com.android.support:design:25.0.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
}