gpt4 book ai didi

android - 实现自 api 17 以来已弃用的滑动抽屉的替代方法

转载 作者:太空狗 更新时间:2023-10-29 15:13:50 26 4
gpt4 key购买 nike

在我的应用程序中,我有一个带有图像按钮的滑动抽屉,单击它会显示图像描述和信息。所以基本上我为此只使用一个 XML 文件和一个 Java 文件。 (但我注意到添加更多图像按钮和法师来显示它需要一段时间才能加载)。现在,由于 API 17 正在弃用滑动抽屉,这让我有点担心该应用程序的 future 下载。现在我的问题是,是否有另一种方法可以在不使用滑动抽屉或微调器的情况下实现这一目标。我真的不想为每个图像创建一个 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>

该页面的一些注释

This layout demonstrates some important layout characteristics:

  • The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content. The main content view is set to match the parent view's width and height, because it represents the entire UI when the navigation drawer is hidden.
  • The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages, specify the value with "start" instead of "left" (so the drawer appears on the right when the layout is RTL).
  • The drawer view specifies its width in dp units and the height matches the parent view. The drawer width should be no more than 320dp so the user can always see a portion of the main content.

主要区别在于 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>

关于android - 实现自 api 17 以来已弃用的滑动抽屉的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14439223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com