微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 从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>

页面的一些注释

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>

原文地址:https://www.jb51.cc/android/312810.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐