我想创建一个导航抽屉效果,但不是抽屉从左边滑出,它必须在主视图的"后面",所以滑动的项目是视图本身(即向右) - 抽屉完全不动,但通过滑动主视图"揭开".手指轻扫(并点击汉堡包图标)动作与抽屉相同,只是揭示效果不同.
主视图的移动方式与此处可实现的方式相同
但在我的情况下,我希望"抽屉"静止地保持在主视图下方.
通过叠加视图和使抽屉透明,我达到了预期的效果 - 当然这意味着抽屉不再用于导航; 只是为了效果.使用上面链接中的代码
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/whole_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- everything here is now the new "drawer" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="BOO I am hiding!"
android:id="@+id/tv2"
android:layout_gravity="left|top" />
<!-- /everything here is now the new "drawer" -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- The This is the main content frame -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="MOOOOOO!"
android:id="@+id/textView"
android:layout_gravity="left|top" />
</FrameLayout>
<!-- The see-through navigation drawer -->
<RelativeLayout
android:layout_width="280dp"
android:layout_height="match_parent"
android:id="@+id/left_drawer"
android:clickable="false"
android:background="@drawable/transparent_bg" <!-- #00FFFFFF -->
android:layout_gravity="start">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
一切都很完美,除了在打开"抽屉"时不能点击id/tv2(或该层上的其他项目).
我试过了:
1)使上面的布局不可点击(drawer_layout,content_frame和left_drawer setClickable(false)没有效果).
2)上面链接中的代码移动X轴上的content_frame而不是drawyer_layout - 我尝试移动drawer_layout而不是框架 - 这不能正常工作,因为你不能再点击最右边再次关闭抽屉.
3)将drawer_layout的android:layout_width更改为fill_parent,但这仍然是一个问题,因为它不是被移动的图层.
我的猜测是DrawerLayout(android.support.v4.widget.DrawerLayout)并不意味着高于另一个布局,我可能需要创建自己的抽屉以达到预期的效果 - 任何想法或建议都会很棒.
我相信我已经找到了一个相对简单的解决方案.以下DrawerLayout设置非常标准 - 即内容ViewGroup优先,抽屉View最后,使用标准属性等.此外,使用类似于您发布的链接中显示的技术来使用抽屉滑动内容.
示例中的技巧是抽屉本身的设置.我们将使用两个嵌套的ViewGroups - 外部的是常规的滑动抽屉View; 内部,一个ViewGroup用于实际的抽屉内容.然后,我们将ViewGroup使用DrawerListener(ActionBarDrawerToggle在我们的示例中)滑动与抽屉滑动方向相反的内部内容,使抽屉在左侧看起来是静止的.
例子DrawerLayout,main.xml.请注意以下设置的标准抽屉属性drawer_container ViewGroup:
<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"
android:background="#000000">
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc" />
<FrameLayout
android:id="@+id/drawer_container"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<LinearLayout
android:id="@+id/drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
...
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
此示例Activity显示了我们需要添加到常规导航抽屉设置的额外几行.比让引用其他ViewS后,主位在ActionBarDrawerToggle的onDrawerSlide()方法:
public class MainActivity extends Activity {
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
View drawerContainer;
View drawerContent;
View mainContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerContainer = findViewById(R.id.drawer_container);
drawerContent = findViewById(R.id.drawer_content);
mainContent = findViewById(R.id.main_content);
toggle = new ActionBarDrawerToggle(...) {
@Override
public void onDrawerSlide(View drawer, float slideOffset) {
super.onDrawerSlide(drawer, slideOffset);
drawerContent.setTranslationX(drawerContainer.getWidth() * (1 - slideOffset));
mainContent.setTranslationX(drawerContainer.getWidth() * slideOffset);
}
};
drawerLayout.addDrawerListener(toggle);
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,DrawerLayout在Activity重新创建期间恢复抽屉打开/关闭状态,因此您可能需要考虑这一点,例如,在方向更改期间等.
| 归档时间: |
|
| 查看次数: |
2742 次 |
| 最近记录: |