【问题标题】:Relative layout and navigation drawer?相对布局和导航抽屉?
【发布时间】:2015-01-21 15:18:43
【问题描述】:

我是 android 新手,我有一个相对布局,我正在为它实现导航抽屉,所以我不知道如何放置它。刷入和刷出不起作用......当我使用 DrawerLayout 和 FrameLayout 创建一个新项目时,它正在工作,但是当我在我的项目上实现它时出现故障

activity_main.xml
 <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" >


<RelativeLayout 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:background="@drawable/calc" >




    <ListView
        android:id="@+id/drawerlist"
        android:background="#00a77f"

        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left" />





    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/linearll1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="30dp"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/cs"
                    android:layout_width="100dp"
                    android:layout_height="140dp"
                    android:layout_weight="1"
                    android:alpha="0.5"
                    android:background="@drawable/roundbutton"
                    android:text="@string/cs"
                    android:textColor="@drawable/recbutcolor"
                    android:textSize="22sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/cal"
                    android:layout_width="100dp"
                    android:layout_height="140dp"
                    android:layout_marginLeft="20dp"
                    android:layout_weight="1"
                    android:alpha="0.5"
                    android:background="@drawable/roundbutton"
                    android:text="@string/maincalc"
                    android:textColor="@drawable/recbutcolor"
                    android:textSize="22sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearll2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearll1"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="30dp"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/interviewquestion"
                    android:layout_width="100dp"
                    android:layout_height="140dp"
                    android:layout_weight="1"
                    android:alpha="0.5"
                    android:background="@drawable/roundbutton"
                    android:text="@string/interview_ques"
                    android:textColor="@drawable/recbutcolor"
                    android:textSize="22sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/careerGuidance"
                    android:layout_width="100dp"
                    android:layout_height="140dp"
                    android:layout_marginLeft="20dp"
                    android:layout_weight="1"
                    android:alpha="0.5"
                    android:background="@drawable/roundbutton"
                    android:text="@string/career_guid"
                    android:textColor="@drawable/recbutcolor"
                    android:textSize="22sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <Button
                android:id="@+id/about"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearll2"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="30dp"
                android:alpha="0.5"
                android:background="@drawable/roundbutton"
                android:gravity="center"
                android:text="@string/aboutme"
                android:textColor="@drawable/recbutcolor"
                android:textSize="22sp"
                android:textStyle="bold" />
        </LinearLayout>
    </ScrollView>

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

这是我的 MainActivity: MainActivity.java

import android.content.Intent;
import android.content.res.Configuration;

import android.os.Bundle;

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;

import android.view.Menu;

import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements
        OnItemClickListener {
    private DrawerLayout drawerLayout;
    private ListView listView;
    private String[] navdra;
    private ActionBarDrawerToggle drawerListner;

    Intent shareintent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navdra = getResources().getStringArray(R.array.navdra);

        listView = (ListView) findViewById(R.id.drawerlist);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_1, navdra));
        listView.setOnItemClickListener(this);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
                R.drawable.ic_drawer, R.string.dopen, R.string.dclose);



        drawerLayout.setDrawerListener(drawerListner);
        getSupportActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);


        Button csv = (Button) findViewById(R.id.cs);
        csv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Semester.class);
                startActivityForResult(intent, 0);
            }
        });

        Button cal = (Button) findViewById(R.id.cal);
        cal.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Calculator.class);
                startActivityForResult(intent, 0);
            }
        });

        Button iq = (Button) findViewById(R.id.interviewquestion);

        iq.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(),
                        InterviewQuestion.class);
                startActivityForResult(intent, 0);
            }
        });

        Button cg = (Button) findViewById(R.id.careerGuidance);
        cg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Careerguid.class);
                startActivityForResult(intent, 0);
            }
        });

        Button abt = (Button) findViewById(R.id.about);
        abt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "PKlabs!!!",
                        Toast.LENGTH_SHORT).show();
                // Intent intent = new Intent
                // (v.getContext(),InterviewQuestion.class);
                // startActivityForResult(intent,0);
            }
        });

    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        drawerListner.onConfigurationChanged(newConfig);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerListner.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuItem item = menu.add("share"); // your desired title here
        item.setIcon(R.drawable.share_icon); // your desired icon here
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                shareintent = new Intent(android.content.Intent.ACTION_SEND);
                shareintent.setType("text/plain");
                String shareBody = "Here is the share content body";
                shareintent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        "Subject Here");
                shareintent.putExtra(Intent.EXTRA_TEXT, "Hi there");
                startActivity(Intent.createChooser(shareintent, "Share via"));
                return true;
            }
        });

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        if(drawerListner.onOptionsItemSelected(item))
        {
            return true;
        }




        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        Toast.makeText(this, navdra[position] + " selected",
                Toast.LENGTH_SHORT).show();
        selectItem(position);
    }

    public void selectItem(int position) {
        // TODO Auto-generated method stub
        listView.setItemChecked(position, true);
        setTitle(navdra[position]);
    }

    public void setTitle(String title) {
        getSupportActionBar().setTitle(title);
    }




    private Toast toast;
    private long lastBackPressTime = 0;

    @Override
    public void onBackPressed() {
        if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
            toast = Toast.makeText(this, "Press back again to exit",
                    Toast.LENGTH_LONG);
            toast.show();
            this.lastBackPressTime = System.currentTimeMillis();
        } else {
            if (toast != null) {
                toast.cancel();
            }
            super.onBackPressed();
        }
    }


}

【问题讨论】:

  • 由于某种原因,如果我使用相对布局,内容不会显示,但如果我切换并使用 LinearLayout,我会看到我的视图的内容。

标签: android android-relativelayout navigation-drawer android-framelayout


【解决方案1】:

DrawerLayout 中的第一个元素是活动内容(在您的情况下为 RelativeLayout)。第二个是抽屉本身(ListView 给你)。

将您的布局更改为:

<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" >

    <RelativeLayout 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:background="@drawable/calc" >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/linearll1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="30dp"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/cs"
                        android:layout_width="100dp"
                        android:layout_height="140dp"
                        android:layout_weight="1"
                        android:alpha="0.5"
                        android:background="@drawable/roundbutton"
                        android:text="@string/cs"
                        android:textColor="@drawable/recbutcolor"
                        android:textSize="22sp"
                        android:textStyle="bold" />

                    <Button
                        android:id="@+id/cal"
                        android:layout_width="100dp"
                        android:layout_height="140dp"
                        android:layout_marginLeft="20dp"
                        android:layout_weight="1"
                        android:alpha="0.5"
                        android:background="@drawable/roundbutton"
                        android:text="@string/maincalc"
                        android:textColor="@drawable/recbutcolor"
                        android:textSize="22sp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearll2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearll1"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="30dp"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/interviewquestion"
                        android:layout_width="100dp"
                        android:layout_height="140dp"
                        android:layout_weight="1"
                        android:alpha="0.5"
                        android:background="@drawable/roundbutton"
                        android:text="@string/interview_ques"
                        android:textColor="@drawable/recbutcolor"
                        android:textSize="22sp"
                        android:textStyle="bold" />

                    <Button
                        android:id="@+id/careerGuidance"
                        android:layout_width="100dp"
                        android:layout_height="140dp"
                        android:layout_marginLeft="20dp"
                        android:layout_weight="1"
                        android:alpha="0.5"
                        android:background="@drawable/roundbutton"
                        android:text="@string/career_guid"
                        android:textColor="@drawable/recbutcolor"
                        android:textSize="22sp"
                        android:textStyle="bold" />
                </LinearLayout>

                <Button
                    android:id="@+id/about"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearll2"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="30dp"
                    android:alpha="0.5"
                    android:background="@drawable/roundbutton"
                    android:gravity="center"
                    android:text="@string/aboutme"
                    android:textColor="@drawable/recbutcolor"
                    android:textSize="22sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

    <ListView
        android:id="@+id/drawerlist"
        android:background="#00a77f"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left" />
</android.support.v4.widget.DrawerLayout>

【讨论】:

  • @PUNEET 你的抽屉应该是什么?仅ListView 还是整个RelativeLayout
  • 一个列表视图,相对布局是我的主页
  • 文档中跟在根元素后面的标记必须格式正确,这个错误来了
  • @Simas 感谢您的回答,我遇到了类似的问题,您的逻辑我解决了。
【解决方案2】:

要使用 DrawerLayout,请将您的主要内容视图定位为第一个 宽度和高度为 match_parent 的孩子。将抽屉添加为子项 主要内容视图之后的视图并设置 layout_gravity 适当地。抽屉通常使用 match_parent 作为高度 固定宽度。

(Android 文档link

将以下行添加到您的相对布局中:

android:layout_gravity="left|start"

【讨论】:

    【解决方案3】:

    那是因为layout_gravity 不适用于RelativeLayout。请改用框架布局。

    编辑

    <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">
    
        <ListView
            android:id="@+id/drawerlist"
            android:background="#00a77f"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"/>
    
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:background="@drawable/calc">
    
    
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <LinearLayout
                        android:id="@+id/linearll1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginTop="30dp"
                        android:orientation="horizontal">
    
                        <Button
                            android:id="@+id/cs"
                            android:layout_width="100dp"
                            android:layout_height="140dp"
                            android:layout_weight="1"
                            android:alpha="0.5"
                            android:background="@drawable/roundbutton"
                            android:text="@string/cs"
                            android:textColor="@drawable/recbutcolor"
                            android:textSize="22sp"
                            android:textStyle="bold"/>
    
                        <Button
                            android:id="@+id/cal"
                            android:layout_width="100dp"
                            android:layout_height="140dp"
                            android:layout_marginLeft="20dp"
                            android:layout_weight="1"
                            android:alpha="0.5"
                            android:background="@drawable/roundbutton"
                            android:text="@string/maincalc"
                            android:textColor="@drawable/recbutcolor"
                            android:textSize="22sp"
                            android:textStyle="bold"/>
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/linearll2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/linearll1"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginTop="30dp"
                        android:orientation="horizontal">
    
                        <Button
                            android:id="@+id/interviewquestion"
                            android:layout_width="100dp"
                            android:layout_height="140dp"
                            android:layout_weight="1"
                            android:alpha="0.5"
                            android:background="@drawable/roundbutton"
                            android:text="@string/interview_ques"
                            android:textColor="@drawable/recbutcolor"
                            android:textSize="22sp"
                            android:textStyle="bold"/>
    
                        <Button
                            android:id="@+id/careerGuidance"
                            android:layout_width="100dp"
                            android:layout_height="140dp"
                            android:layout_marginLeft="20dp"
                            android:layout_weight="1"
                            android:alpha="0.5"
                            android:background="@drawable/roundbutton"
                            android:text="@string/career_guid"
                            android:textColor="@drawable/recbutcolor"
                            android:textSize="22sp"
                            android:textStyle="bold"/>
                    </LinearLayout>
    
                    <Button
                        android:id="@+id/about"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/linearll2"
                        android:layout_gravity="center"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginTop="30dp"
                        android:alpha="0.5"
                        android:background="@drawable/roundbutton"
                        android:gravity="center"
                        android:text="@string/aboutme"
                        android:textColor="@drawable/recbutcolor"
                        android:textSize="22sp"
                        android:textStyle="bold"/>
                </LinearLayout>
            </ScrollView>
    
    
        </FrameLayout>
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • @PUNEET 请立即测试
    • 属性缺少 Android 命名空间前缀,错误
    • 检查是否真的复制粘贴正确。所有属性都有android 前缀。
    • 没问题。如果您需要一个用于材料设计工作的模板,请在导航抽屉中查看我的项目:github.com/kanytu/android-material-drawer-template
    猜你喜欢
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多