3

私はアンドロイドが初めてで、ナビゲーションドロワーを使用して練習用アプリを開発しています。メイン ビューを 3 つのタブを持つページャーにしたいのですが、ユーザーが左から右にスライドすると、ナビゲーション ドロワーが表示される可能性があります。

ここに私のMain.xmlコードがあります:

<android.support.v4.widget.DrawerLayout 
    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"
    android:id="@+id/main_layout"
    tools:context=".ExampleMain" >

<!-- 
    Main Content place holder
 -->
<FrameLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<!-- 
    Left Drawer main style
 -->
<ListView
    style="@style/HighlightColor"
    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.support.v4.widget.DrawerLayout>

別のxml Main_Content.xml<Framelayout>で定義された実際のメインビューを含める必要があります

<andriod.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_content_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Main_Content.xmlViewpagerMain.xml<FrameLayout>セクションに配置する方法がわかりません。サンプルコードまたはポインターを提供してください。ありがとうございました

4

3 に答える 3

1

次の方法で使用できます。

<android.support.v4.widget.DrawerLayout 
        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"
        android:id="@+id/main_layout"
        tools:context=".ExampleMain" >

    <!-- 
        Main Content place holder
     -->
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <andriod.support.v4.view.ViewPager
      android:id="@+id/main_content_pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

    </FrameLayout>

    <!-- 
        Left Drawer main style
     -->
    <ListView
        style="@style/HighlightColor"
        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.support.v4.widget.DrawerLayout>

設計どおりの NavigationDrawer は、画面の左 (または右) 部分の 20 dp マージンからスワイプしようとすると表示されるように設計されています。

EDIT 1: ビューを別々のxmlに入れたい場合は、それを実行して実行時にビューを追加することができます。

次のように別の xml を記述します

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
   <android.support.v4.widget.DrawerLayout 
        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"
        android:id="@+id/main_layout" >

    <!-- 
        Main Content place holder
     -->
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <!-- 
        Left Drawer main style
     -->
    <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.support.v4.widget.DrawerLayout>

次に、アクティビティの onCreate で、次のコードを実行できます。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
                ViewPage pager = (ViewPager) LayoutInflater.from(this).inflate(R.layout.view_pager, null, false);
                ViewGroup container = (ViewGroup) findViewById(R.id.main_content);
                container.addView(pager);
               }

お役に立てば幸いです。

于 2013-11-05T04:52:15.013 に答える
0

レイアウトを拡張して動的に行うか、静的レイアウトの場合は include タグを使用できます。

静的なものの場合:

<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/your_layout"/>
</FrameLayout>

しかし、ナビゲーション ドローを使用しているので、必要なのは動的なものであることを願っています。コードから、あなたがする必要があるのは次のとおりです。

private void onNavigationClicked(){
    View v=LayoutInflater.from(this).inflate(R.layout.your_layout,null);
    FrameLayout fl = (FrameLayout)findViewById(R.layout.main_content);
    fl.removeAllViews();
    fl.addView(v);
    fl.invalidate();
}
于 2013-11-05T05:02:16.087 に答える