12

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

任何人都可以请帮助或对做什么有建议吗?

4

6 回答 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>
于 2013-08-01T21:01:19.917 回答
1

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

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

编辑:这个看起来也很有前途:https : //github.com/6wunderkinder/android-sliding-layer-lib演示应用程序,你会看到它也可以从下到上和从上到下)

于 2014-04-24T09:37:48.740 回答
1

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

我使用的概念

滑块按钮和内容面板

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

我是怎么做到的

我玩的是左边距,所以当你按下滑块按钮时,内容窗格(最初隐藏)变得与 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>
于 2013-08-22T06:09:07.623 回答
1

我认为是一个很好的替代
AFAIK 它不使用SlidingDrawer,您可以修改抽屉的方向

于 2013-01-01T02:47:03.090 回答
0

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

于 2014-01-30T00:12:37.710 回答
0

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

布局

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

需要注意的两点是:

  • 注意app:layout_behaviorFrameLayout样式
  • 为了使工作表工作,它需要包含在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)
于 2022-03-04T21:31:10.233 回答