【问题标题】:Alternative way for implementing a slidingdrawer that has now been deprecated since api 17实现自 api 17 以来已弃用的滑动抽屉的替代方法
【发布时间】:2012-11-25 13:45:04
【问题描述】:

在我的应用程序中,我有一个带有图像按钮的滑动抽屉,单击它会显示图像描述和信息。所以基本上我只使用了一个 XML 文件和一个 Java 文件。 (但我注意到添加更多图像按钮和法师来显示需要一段时间才能加载)。现在,由于 API 17 不推荐使用滑动抽屉,这让我对应用程序的未来下载有点担心。现在我的问题是,是否有另一种方法可以在不使用滑动抽屉或微调器的情况下实现这一目标。我真的不想为每个图像创建一个 xml 和 java 文件(我最终会得到 100 多个 xml 和 java) 这是我目前拥有的代码。

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">

<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();
        } 
    });
    ............

任何人都可以帮忙或对如何做提出建议吗?

【问题讨论】:

    标签: android android-layout android-intent


    【解决方案1】:

    这是左边的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>
    

    【讨论】:

      【解决方案2】:

      我认为This 是一个不错的选择
      AFAIK 它不使用SlidingDrawer,您可以修改抽屉的方向

      【讨论】:

        【解决方案3】:

        来自应用程序 Umano 的人的 SlidingUpPanel 似乎是目前最好的方法。您可以在以下位置找到它:https://github.com/umano/AndroidSlidingUpPanel

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

        编辑:这个看起来也很有前途:https://github.com/6wunderkinder/android-sliding-layer-lib (在 youtube 视频中似乎只是从右到左和从左到右工作,但如果你下载实际的演示应用程序,你会发现它也可以从下到上和从上到下)

        【讨论】:

          【解决方案4】:

          我宁愿推荐一个我自己创建的简单滑动菜单。

          我使用的概念

          滑块按钮和内容面板

          最初滑块按钮在左侧(在我的示例中),当您单击它时,它会移动并且内容窗格可见

          我是如何做到的

          我玩的是左边距,所以当你按下滑块按钮时,内容窗格(最初隐藏)变得与 screen_width/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>
          

          【讨论】:

            【解决方案5】:

            普通的 android 你可以使用 DrawerLayout。 但我推荐SlidingMenu lib 对用户和程序员有更好的可用性。

            【讨论】:

            • DrawerLayout 也适用于旧的 Android 版本,因为它在 v4 支持库中。
            • @A. Binzxxxxxx ,你将如何使用抽屉布局从底部拉菜单?
            • 大约 6 年以来没有编写任何 android - 抱歉
            【解决方案6】:

            大多数答案都已经很老了。如果您正在使用 API 30/31,则应该改用工作表。如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
            • 为了使工作表工作,需要将其包含在 CoordinatorLayout

            示例片段:

            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)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-09-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-12-07
              • 2018-02-11
              • 2019-12-15
              相关资源
              最近更新 更多