滑动抽屉出现在所有活动中

发布于 2024-10-16 00:39:57 字数 193 浏览 4 评论 0原文

我正在开发一个包含许多活动的应用程序 我用滑动抽屉创建了自己的菜单(我不想使用内置菜单按钮) 由于滑动抽屉位于屏幕底部并包含我的菜单按钮,

我需要的是使滑动抽屉出现在我的所有活动中,

我尝试创建一个活动并将其内容视图设置为包含抽屉的 xml 文件然后将该活动扩展到所有其他活动,但此解决方案不起作用,

所以有什么建议吗?

I am developing an application that contains many activities
and i created my own menu (i don't want to use the built in menu button) with the Sliding Drawer
as the sliding drawer is at the bottom of the screen and contains my menu buttons

what i need is to make that sliding drawer to appear in all my activities

i tried to create an activity and set it's content view to the xml file that includes the drawer and then extends that activity in all other activities but this solution doesn't work

so any suggestions ?

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

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

发布评论

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

评论(3

撕心裂肺的伤痛 2024-10-23 00:39:57

扩展才是正确的方法。只需以正确的方式重写 setContentView 即可。
这是工作示例,但我不是使用抽屉,而是使用创建的自定义选项卡栏:

使用抽屉定义布局,如下所示:

这是 act_layout.xml

<LinearLayout
  ...
  android:orientation="vertical"
>
  <YourDrawer
    ...
  />
  <FrameLayout
    android:id="@+id/act_content"
    ...
  >
    // Here will be all activity content placed
  </FrameLayout>
</LinearLayout>

这将是包含所有其他布局的基本布局在 act_content 框架中。
接下来,创建一个基础活动类,并执行以下操作:

public abstract class DrawerActivity extends Activity {

    protected LinearLayout fullLayout;
    protected FrameLayout actContent;

    @Override
    public void setContentView(final int layoutResID) {
        // Your base layout here
        fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null); 
        actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);

        // Setting the content of layout your provided to the act_content frame
        getLayoutInflater().inflate(layoutResID, actContent, true); 
        super.setContentView(fullLayout);

        // here you can get your drawer buttons and define how they 
        // should behave and what must they do, so you won't be 
        // needing to repeat it in every activity class
    }
}

我们所做的基本上是拦截对 setContentView(int resId) 的所有调用,从 xml 膨胀我们的抽屉布局,膨胀我们的活动布局(通过方法调用中提供的 reId) ,根据需要组合它们,并设置为activity的contentView。

编辑:
创建完上面的内容后,只需像往常一样继续编写应用程序,创建布局(不提及抽屉)创建活动,但不要扩展简单的活动,而是扩展 DrawerActivity,如下所示:

public abstract class SomeActivity extends DrawerActivity {

    protected void onCreate(Bundle bundle) {
        setContentView(R.layout.some_layout);
    }
}

会发生什么,setContentView (R.layout.some_layout) 被拦截。您的 DrawerActivity 加载您从 xml 提供的布局,加载抽屉的标准布局,组合它们,然后将其设置为活动的 contentView。

Extending is the right way. Just override setContentView in the right way.
Here's the working example, but instead of drawer, I use a created a custom tabbar:

Define a layout with your drawer like this:

this is act_layout.xml

<LinearLayout
  ...
  android:orientation="vertical"
>
  <YourDrawer
    ...
  />
  <FrameLayout
    android:id="@+id/act_content"
    ...
  >
    // Here will be all activity content placed
  </FrameLayout>
</LinearLayout>

This will be your base layout to contain all other layouts in the act_content frame.
Next, create a base activity class, and do the following:

public abstract class DrawerActivity extends Activity {

    protected LinearLayout fullLayout;
    protected FrameLayout actContent;

    @Override
    public void setContentView(final int layoutResID) {
        // Your base layout here
        fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null); 
        actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);

        // Setting the content of layout your provided to the act_content frame
        getLayoutInflater().inflate(layoutResID, actContent, true); 
        super.setContentView(fullLayout);

        // here you can get your drawer buttons and define how they 
        // should behave and what must they do, so you won't be 
        // needing to repeat it in every activity class
    }
}

What we do, is basically intercept all calls to setContentView(int resId), inflate our layout for drawer from xml, inflate our layout for activity (by reId provided in method call), combine them as we need, and set as the contentView of the activity.

EDIT:
After you've created the stuff above, just proceed to write an app as usual, create layouts (without any mention of a drawer) create activities, but instead of extending simple activity, extend DrawerActivity, like so:

public abstract class SomeActivity extends DrawerActivity {

    protected void onCreate(Bundle bundle) {
        setContentView(R.layout.some_layout);
    }
}

What happens, is that setContentView(R.layout.some_layout) is intercepted. Your DrawerActivity loads the layout you provided from xml, loads a standart layout for your drawer, combines them and then sets it as contentView for the activity.

数理化全能战士 2024-10-23 00:39:57

三年后,对于那些可能没有完全听从奥尔洛夫先生的回答的人来说,这是这个重要问题的完整解决方案。

他制作层次视图的方法完全没问题,但有一些小错误可能会误导初学者开发人员。

  1. 正如 Gabe 也提到的,你可以在声明中去掉抽象。
  2. 不需要用 FrameLayout 包裹两个子元素。父级可以是任何RelativeLayout、LinearLayout 等。但最重要的部分是您必须在Slider 之前声明FrameLayout。

所以100%工作的解决方案是这样的:

activity_drawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<YourDrawer
    android:id="@+id/drawer_drawer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

</YourDrawer>

</RelativeLayout>

DrawerActivity.java

public class DrawerActivity extends Activity {

protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;

@Override
public void setContentView(int layoutResID) {

    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
    frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

    getLayoutInflater().inflate(layoutResID, frameLayout, true);

    super.setContentView(fullLayout);

    //Your drawer content...

}

}

MainActivity.java

public class MainActivity extends DrawerActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

}

不要忘记在清单中声明DrawerActivity。

希望有帮助。

Finally after 3 years, here's the complete solution to this important question for whom may have not been completely guided by Mr. Orlov's answer.

His method for making a hierarchy view was completely OK but there was some small mistakes that may mislead beginner developers.

  1. As Gabe also mentioned, you can get rid of abstract in declaration.
  2. There's no need to wrap both child with a FrameLayout. The parent could be anything RelativeLayout, LinearLayout, etc. But the most important part is that you must declare the FrameLayout before the Slider.

So the 100% working soloution would be like this :

activity_drawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<YourDrawer
    android:id="@+id/drawer_drawer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

</YourDrawer>

</RelativeLayout>

DrawerActivity.java

public class DrawerActivity extends Activity {

protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;

@Override
public void setContentView(int layoutResID) {

    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
    frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

    getLayoutInflater().inflate(layoutResID, frameLayout, true);

    super.setContentView(fullLayout);

    //Your drawer content...

}

}

MainActivity.java

public class MainActivity extends DrawerActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

}

Don't forget to declare DrawerActivity in manifest.

Hope it helped.

非要怀念 2024-10-23 00:39:57

我知道这已经晚了两年,但对于那些想在这里得到答案的人来说,这已经晚了。奥尔洛夫先生做出了适当的回应,只需要进行一些调整。

  • 删除公开后的摘要。 (这就是导致错误的原因)
  • 用另一个 FrameLayout 包装滑动抽屉和 FrameLayout 并将宽度和高度设置为 match_parent。 (这允许出现两种布局)。

另外:如果您希望滑动抽屉出现在布局的顶部(废话!),请将位于滑动抽屉之后的frameLayout 放置在滑动抽屉之前。

I know this is two years late but for those who would like an answer here it is. Mr Orlov had an appropriate response, just a few adjustments needed.

  • delete the abstract after public. (this is what causes an error)
  • wrap BOTH your sliding drawer and FrameLayout with another FrameLayout and set the width and height to match_parent. (This allows BOTH layouts to appear).

ALSO: If you want your sliding drawer to appear on top of your layout (duh!) then place the frameLayout that is after the slidingDrawer before the slidingDrawer.

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