实现滑动抽屉的另一种方式,自api 17以来一直被弃用



在我的应用程序中,我有一个滑动抽屉,里面有图像按钮,点击后会显示图像描述和信息。因此,基本上我只使用了一个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();
} 
});
............

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

这是左边的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顺序和抽屉必须在内容的上面。设置了主要内容视图以匹配父视图的宽度和高度,因为它表示隐藏导航抽屉时的整个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>

我认为这是一个很好的选择
AFAIK它不使用SlidingDrawer,你可以修改抽屉的方向

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

我在另一篇SOF文章中找到了关于它的信息:垂直抽屉布局或滑动窗格布局:D

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

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

我使用的概念

滑块按钮和内容面板

最初的滑块按钮在左边(在我的例子中),当你点击它时,它会移动,内容窗格会变成可见的

我是如何做到这一点的

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

普通android,您可以使用DrawerLayout。但我推荐SlidingMenu库,它对用户和程序员来说有更好的可用性。

大多数答案都很古老。如果您正在使用API 30/31,则应使用工作表。如安卓材料设计文档所述

布局

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

需要注意的两件事是:

  • 请注意FrameLayout应用程序:layout_behavior样式
  • 为了使工作表工作,需要将其包含在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)

最新更新