Android底表布局XML [英] Android Bottom Sheet Layout XML

查看:49
本文介绍了Android底表布局XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试实现一个简单的Android底部工作表,而我使用的xml如下所示!

Hello there I am trying to implement a simple Android Bottom sheet and my used xml is as follow!

<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

           <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Effects"
              android:id="@+id/effects"
              android:textStyle="italic"/>

    <!--contains my layout for buttons, nested layout etc etc-->
    </LinearLayout

    <android.support.v4.widget.NestedScrollView
       android:id="@+id/color_effects_bottom_sheet"
       android:layout_width="match_parent"
       android:layout_height="350dp"
       android:clipToPadding="true"
       app:behavior_hideable="true"
       android:background="@android:color/holo_orange_light"
      app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Please Set the Color Effects here!"
        android:padding="16dp"
        android:textSize="16sp"/>

</android.support.v4.widget.NestedScrollView>

此XML背后的代码如下

The Code behind this XML is as Follow

公共类MainActivity扩展了活动{

public class MainActivity extends Activity {

//Variables for bottom sheets calls
private Button btn_effects;
private BottomSheetBehavior mBottomSheetBehavior;
private CoordinatorLayout coordinatorLayout;
////////////////////////////////////

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

    //locate coordinator layout
    coordinatorLayout=(CoordinatorLayout)findViewById(R.id.main_content);

    //locate bottom sheet
    View color_bottomSheet = coordinatorLayout.findViewById( R.id.color_effects_bottom_sheet);

    //View scene_bottomSheet = coordinatorLayout.findViewById( R.id.scenes_bottom_sheet);

    //effects button
    btn_effects=(Button)findViewById(R.id.effects);

    //scenes button
    btn_scenes=(Button)findViewById(R.id.scenes);

    //settings button
    btn_settings=(Button)findViewById(R.id.settings);

    //bottom sheet behavior
    mBottomSheetBehavior = BottomSheetBehavior.from(color_bottomSheet);

    //effects button listener
    btn_effects.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });

}

}

问题在于,当我尝试运行此代码时,它会崩溃我的App并始终抛出此异常.

The problem is that when i try to run this code it crashes my App and throws this Exception all the time.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bottomsheetexample, PID: 22875 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomsheetexample/com.example.bottomsheetexample.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:409)
at android.view.LayoutInflater.inflate(LayoutInflater.java:358)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305)
at android.app.Activity.setContentView(Activity.java:1959)
at com.example.junaidhassan.bottomsheetexample.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5310)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5551) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730) 
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 android.view.LayoutInflater.createView(LayoutInflater.java:614)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:409) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:358) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305) 
at android.app.Activity.setContentView(Activity.java:1959) 
at com.example.bottomsheetexample.MainActivity.onCreate(MainActivity.java:23) 
at android.app.Activity.performCreate(Activity.java:5310) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5551) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730) 
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:34)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:182)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:176)
at java.lang.reflect.Constructor.constructNative(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:409) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:358) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305) 
at android.app.Activity.setContentView(Activity.java:1959) 
at com.example.bottomsheetexample.MainActivity.onCreate(MainActivity.java:23) 
at android.app.Activity.performCreate(Activity.java:5310) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5551) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730) 
at dalvik.system.NativeStart.main(Native Method) 


需要

基本上,我是第一次尝试实现底表.我想添加多个底部表单,以了解如何在应用程序中使用它们.


Need

Basically I am trying to implement Bottom Sheets for the first time. I want to add multiple Bottom Sheets to learn how to use them in the Application.

我的活动"是具有以下应用主题的全屏活动

My Activity is a Full Screen Activity with the following App Theme

android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen">

我想使用全屏活动",并希望在按钮单击侦听器上实现多个底页!

I want to use the Full Screen Activity and want to implement multiple Bottom Sheets on Button Click Listeners!

推荐答案

 <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
   <item name="windowNoTitle">true</item>
   <item name="windowActionBar">false</item>
   <item name="android:windowFullscreen">true</item>
   <item name="android:windowContentOverlay">@null</item>
 </style>

我必须设置自定义样式以使用全屏活动来使用多个底部屏幕!

I have to set a Custom Style to use a full screen activity to use multiple Bottom Screen!

<?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"
     android:id="@+id/main_content"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true">

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

           <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Effects"
              android:id="@+id/effects"
              android:textStyle="italic"/>

           <!--contains my layout for buttons, nested layout etc etc-->
    </LinearLayout

    <android.support.v4.widget.NestedScrollView
       android:id="@+id/color_effects_bottom_sheet"
       android:layout_width="match_parent"
       android:layout_height="350dp"
       android:clipToPadding="true"
       app:behavior_hideable="true"
       android:background="@android:color/holo_orange_light"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Please Set the Color Effects here!"
        android:padding="16dp"
        android:textSize="16sp"/>

</android.support.v4.widget.NestedScrollView>


 <android.support.v4.widget.NestedScrollView
    android:id="@+id/second_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:clipToPadding="true"
    app:behavior_hideable="true"
    android:background="@android:color/holo_orange_light"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

       <TextView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="Please Set the Scene Effects here!"
         android:padding="16dp"
         android:textSize="16sp"/>

    </android.support.v4.widget.NestedScrollView>

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


问题是,人们可以像调用第一个底部表一样调用第二个底部表.


The thing is that one can invoke the second bottom sheet same as of the first bottom sheet.

我的问题是使用全屏活动,并且正在使用非AppCompat主题和活动.因此,我使用了自定义样式主题活动,就可以了!

My problem was to use full screen activity, and was using non AppCompat Theme and Activity. So I used a custom style themed activity and here it goes!

这篇关于Android底表布局XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆