如何使FAB在活动的片段而不是AppCompatActivity中工作 [英] How to make FAB work in Fragment from a Activity and not in AppCompatActivity
问题描述
我将FAB放在活动的片段中,并且不断出现此错误:
I'm putting my FAB in a Fragment from an Activity and I keep getting this error:
10-01 21:01:55.238 22031-22031/com.vei.scanandgo E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.vei.scanandgo, PID: 22031
android.view.InflateException: Binary XML file line #408: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.vei.scanandgo.BaseFragment.storeView(BaseFragment.java:317)
. . .
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
. . .
. ...
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:159)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:153)
. .
. ..
等级设置为
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
膨胀的完成方式:
View mView = inflater.inflate(getlayoutResId(), container, false);
我唯一没有更改的是将Activity更改为AppCompatActivity,它将击中许多现有代码. 我们有办法使该FAB与Activity一起使用吗?
The only thing that I haven't changed is the Activity to AppCompatActivity which will hit a lot of existing codes. Do we have a way to make this FAB work with Activity?
FAB:
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/pichere"
app:backgroundTint="@color/white"
app:borderWidth="0dp" />
谢谢!
如果我做错了设计,请告诉我我可以做些什么,以使FAB可以制作此片段.
If I am making a wrong design, please advise on what else I can do to make this fragment with FAB work.
推荐答案
我们有没有办法使该FAB与Activity一起使用?
Do we have a way to make this FAB work with Activity?
不直接. android.support.design.widget.FloatingActionButton
需要AppCompatActivity
和Theme.AppCompat
.
我在此库中有一个与该小部件交叉的端口, Activity
(或FragmentActivity
),但minSdkVersion
为21或更高,因为它需要Theme.Material
.
I have a cross-port of that widget in this library that works with a regular Activity
(or FragmentActivity
), but it is for a minSdkVersion
of 21 or higher, as it requires Theme.Material
.
还有其他FAB实施,例如此,不需要appcompat-v7
和不需要Theme.Material
.
There are other FAB implementations, such as this one, that do not require appcompat-v7
and do not require Theme.Material
.
这篇关于如何使FAB在活动的片段而不是AppCompatActivity中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!