如何在代码中创建滑动抽屉?

发布于 2024-09-01 03:35:31 字数 115 浏览 7 评论 0原文

我正在尝试在代码中创建一个滑动抽屉,但我不明白如何处理构造函数的 AttributeSet 部分。

我需要为此做什么?

另外,如何在代码中定义滑块将显示的位置?

谢谢,

I'm trying to create a sliding drawer in code, but I don't understand what to do for the AttributeSet part of the constructor.

What do I need to do for that?

Also, how do I define in code where the slider is going to show up?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-09-08 03:35:31

SlidingDrawer 不能在 Java 代码中 new,因为它必须定义句柄和内容,但您可以在 XML 布局中进行膨胀,如下所示:

sliding_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:handle="@+id/handle"
    android:content="@+id/content">
    <ImageView
        android:id="@id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/tray_handle_bookmark"
        />
    <LinearLayout
        android:id="@id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#FF000000"
        />
</SlidingDrawer>

在 Java 代码中:

// you main Layout
LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        mainLayout.setOrientation(LinearLayout.VERTICAL);

// add sliding Drawer
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        slidingDrawer = (SlidingDrawer)inflater.inflate(R.layout.sliding_drawer, mainLayout, false);
        mainLayout.addView(slidingDrawer);

// get Layout for place your content in sliding drawer
LinearLayout slideContent = (LinearLayout)slidingDrawer.findViewById(R.id.content);

slideContent.addView(.....); // add your view to slideDrawer

SlidingDrawer can't new in Java code because it must define handle and content but you can inflate in XML layout as follows:

sliding_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:handle="@+id/handle"
    android:content="@+id/content">
    <ImageView
        android:id="@id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/tray_handle_bookmark"
        />
    <LinearLayout
        android:id="@id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#FF000000"
        />
</SlidingDrawer>

In Java code:

// you main Layout
LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        mainLayout.setOrientation(LinearLayout.VERTICAL);

// add sliding Drawer
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        slidingDrawer = (SlidingDrawer)inflater.inflate(R.layout.sliding_drawer, mainLayout, false);
        mainLayout.addView(slidingDrawer);

// get Layout for place your content in sliding drawer
LinearLayout slideContent = (LinearLayout)slidingDrawer.findViewById(R.id.content);

slideContent.addView(.....); // add your view to slideDrawer
无所的.畏惧 2024-09-08 03:35:31

看起来 SlidingDrawer 无法直接在 Java 代码中创建。您需要在 XML 布局中定义它并扩充该布局。

对不起!

It looks like SlidingDrawer cannot be created directly in Java code. You will need to define it in an XML layout and inflate that layout.

Sorry!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文