簡體   English   中英

將導航抽屜的Listview更改為任何自定義視圖(說相對布局)?

[英]Change Listview of Navigation Drawer to any Custom View (Say Relative Layout)?

我們有要在Android Studio的默認NavigationDrawer中顯示的ListView 我們可以將ListView更改為任何其他Relative Layout嗎?

我們可以寫任何自定義的東西嗎?

是的,您可以將ListView替換為任何其他customView(例如RelativeLayout等)。請參閱我的示例

在您的activity_main.xml編寫一個自定義的relativeLayout然后在MainActivitydrawerLayout版式中提供布局ID,如下所示:

activity_main.xml

    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#0099cc"
            android:id="@+id/view" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/menu_imageView"
            android:src="@drawable/menu"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_alignParentStart="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/zone_name"
            android:text="Kitchen"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_alignBottom="@+id/menu_imageView"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>



    <!-- The navigation drawer -->

    <RelativeLayout
        android:id="@+id/dockedLayout"
        android:layout_width="500dp"
        android:background="@drawable/background_dock"
        android:layout_height="match_parent"
        android:layout_gravity="start">
        <TextView
            android:id="@+id/master_volume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Master Volume"
            android:textColor="#ffffff"
            android:textSize="15dp"
            android:textStyle="bold"/>
        <SeekBar
            android:id="@+id/seekBarMasterVolume"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_below="@+id/master_volume"
            android:progressDrawable="@drawable/progress"
            android:thumb="@drawable/thumb"/>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    RelativeLayout mDockLayout;
    ImageView menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        menu = (ImageView) findViewById(R.id.menu_imageView);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDockLayout = (RelativeLayout) findViewById(R.id.dockedLayout);

        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(mDockLayout);
            }
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM