自API 17以来,已经弃用了实现滑动抽屉的替代方法。

13

我在我的应用程序中有一个带有图像按钮的滑动抽屉,当点击按钮时,会显示图像描述和信息。因此,基本上我只使用一个XML文件和一个Java文件来实现这一点。(但是我已经注意到,添加更多的图像按钮和图像以显示它需要一些时间来加载)。现在,由于 API 17 已经将滑动抽屉弃用,这让我对应用程序的未来下载感到有些担忧。那么我的问题是,是否有一种替代方法来实现这一点,而不使用滑动抽屉或微调器?我真的不想为每个图像创建一个 XML 和 Java 文件(我最终将拥有 100 多个 XML 和 Java 文件)。

这是我目前的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <RelativeLayout 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/iM1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitStart"
    android:adjustViewBounds="true"/>

</RelativeLayout>
</ScrollView>

<SlidingDrawer
    android:id="@+id/sD1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:content="@+id/content"
    android:handle="@+id/handle">

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/icon_1" />

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="@drawable/icon_background1">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <Button
                    android:id="@+id/asample"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/imageicon1"/>
                   .....

而Java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.campsites);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    final SlidingDrawer slider = (SlidingDrawer) findViewById(R.id.sD1);
    final ImageView imageView = (ImageView) findViewById(R.id.iM1);
    slider.animateOpen();

    Button next = (Button) findViewById(R.id.asample);
    Button next1 = (Button) findViewById(R.id.bsample);
    ..........

    next.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            imageView.setImageDrawable(getResources().getDrawable(R.drawable.asample));
            slider.animateClose();
        } 
    });
    next1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            imageView.setImageDrawable(getResources().getDrawable(R.drawable.bsample));
            slider.animateClose();
        } 
    });
    ............

有人能帮忙或提出建议吗?

6个回答

3
这是一个从左侧滑出的SlidingDrawer,对吗?如果是的话,您可以了解一下DrawerLayout
这是Android支持库的一部分,您应该可以相对简单地用它来替换您的XML,并向后兼容到API4。
在该页面中,有一个示例。
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

以下是该页面的一些注释:

此布局演示了一些重要的布局特性:

  • 主内容视图(上面的FrameLayout)必须是DrawerLayout中的第一个子项,因为XML顺序意味着z-ordering, 抽屉必须在内容之上。当导航抽屉隐藏时,主内容视图设置为与父视图的宽度和高度匹配,因为它代表整个UI。
  • 抽屉视图(即ListView)必须使用android:layout_gravity属性指定其水平方向的重力。为了支持从右到左(RTL)的语言, 应将值指定为“start”而不是“left”(这样当布局是RTL时,抽屉就会出现在右侧)。
  • 抽屉视图以dp为单位指定其宽度,并且其高度与父视图匹配。抽屉宽度不应超过320dp,以便用户始终能够看到主内容的一部分。

主要区别在于DrawerLayout是顶级的,您可以将XML放在其中。例如(完全未经测试):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- your content surrounded by a layout to signify that it's actually content -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView 
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
           <RelativeLayout 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"> 

                <ImageView 
                    android:id="@+id/iM1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitStart"
                    android:adjustViewBounds="true"/>

            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>
    <!-- your sliding menu in its own layout -->
    <LinearLayout 
        android:layout_gravity="start"
        android:layout_width="240dp"
        android:layout_height="wrap_content"> 
        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_1" />

        <RelativeLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:background="@drawable/icon_background1">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <Button
                        android:id="@+id/asample"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/imageicon1"/>
                       .....
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

1

我认为这个是一个不错的替代品
据我所知,它不使用SlidingDrawer,而且你可以修改抽屉的方向


1

目前来看,来自Umano应用程序团队的SlidingUpPanel似乎是最好的方法。您可以在以下链接中找到它:https://github.com/umano/AndroidSlidingUpPanel

我在另一个SOF帖子中找到了有关它的信息:垂直DrawerLayout或SlidingPaneLayout :D

编辑:这个也看起来非常有前途:https://github.com/6wunderkinder/android-sliding-layer-lib (在YouTube视频中似乎只能从右到左和从左到右工作,但如果您下载实际的演示应用程序,您将看到也可以从底部向上和从顶部向下移动)


1
我建议使用一个简单的滑动菜单,我自己创建了这个菜单。
我使用的概念是滑动按钮和内容面板。
最初,滑动按钮位于左侧(在我的示例中),当您单击它时,它会移动并使内容面板可见。
我是通过调整左边距来实现这一点的,因此当您按下滑动按钮时,内容面板(最初隐藏)变为屏幕宽度/3,再次按下时则隐藏。
这是我的代码。
public class MainActivity extends Activity  {
    boolean toggle_open=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void open(View v){
        switch (v.getId()) {
        case R.id.imageButton1:
            if(!toggle_open){
                RelativeLayout header=(RelativeLayout)findViewById(R.id.header);
                Display size=getWindow().getWindowManager().getDefaultDisplay();
                int widthby2=size.getWidth()/3;
                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
                lp.setMargins(size.getWidth()/2, 0, 0, 0);
                header.setLayoutParams(lp);
            
                RelativeLayout slider=(RelativeLayout)findViewById(R.id.panel);
                slider.setLayoutParams(new RelativeLayout.LayoutParams((size.getWidth()/2),size.getHeight()));
                slider.setVisibility(View.VISIBLE);
            
                toggle_open=true;
            }
            else{
                RelativeLayout header=(RelativeLayout)findViewById(R.id.header);
                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
                lp.setMargins(0, 0, 0, 0);
                header.setLayoutParams(lp);
                
                RelativeLayout slider=(RelativeLayout)findViewById(R.id.panel);
                slider.setVisibility(View.GONE);
                toggle_open=false;
                    
            }
            break;

        default:
            break;
        }
    }

}

布局XML代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/header"
        android:background="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:padding="20dp" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@android:color/black"
            android:onClick="open"
            android:src="@android:drawable/ic_dialog_dialer" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageButton1"
            android:layout_toRightOf="@+id/imageButton1"
            android:text="Admin Panel"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/panel"
        android:visibility="gone"
        android:layout_toLeftOf="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@android:color/darker_gray"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <fragment class="com.example.avanse.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent"/>

    </RelativeLayout>

</RelativeLayout>

0

大多数答案都比较老旧。如果您正在使用API 30/31,则应改用Sheet。如Android Material design documentation所述。

布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
  ...>

  <FrameLayout
    ...
    android:id="@+id/standard_bottom_sheet"
    style="?attr/bottomSheetStyle"    
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<!-- Contents -->
   <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/black"
        android:onClick="open"
        android:src="@android:drawable/ic_dialog_dialer" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_toRightOf="@+id/imageButton1"
        android:text="Admin Panel"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white" />
<!-- Contents End -->

  </FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

需要注意的两个事项是:

  • 注意FrameLayoutapp:layout_behaviorstyle
  • 为了使Sheet正常工作,它需要被包含在一个CoordinatorLayout

示例Fragment:

class ModalBottomSheet : BottomSheetDialogFragment() {

   override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.modal_bottom_sheet_content, container, false)

   companion object {
       const val TAG = "ModalBottomSheet"
   }
}

活动代码: 您可以使用以下方法通过编程方式显示表格...

val modalBottomSheet = ModalBottomSheet()
modalBottomSheet.show(supportFragmentManager, ModalBottomSheet.TAG)

0

对于普通的Android应用,您可以使用DrawerLayout。 但我推荐SlidingMenu lib,它对用户和程序员都有更好的可用性。


DrawerLayout也可以与旧版Android一起使用,因为它在v4支持库中。 - NasaGeek
@A. Binzxxxxxx,您如何使用DrawerLayout从底部拉出菜单? - juztcode
我已经有大约6年没有编写任何Android代码了 - 抱歉。 - A. Binzxxxxxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接