如何从drawable菜单更改活动?

时间:2018-06-14 15:04:46

标签: android

我在主要活动中创建了Drawable布局。 现在我想从可绘制布局中将主要活动更改为 aboutUs 活动,我在其中声明了一个名为 nav_about

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_height="match_parent" android:layout_width="match_parent">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_about"
        android:icon="@mipmap/about_round"
        app:showAsAction="always"
        android:title="About" />
</group>
</menu>

我希望在可绘制的菜单中点击这个时称为aboutUs的新活动。

1 个答案:

答案 0 :(得分:0)

您可以做的是将该代码复制到适当的布局中:

activity_main.xml中:

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

<LinearLayout
    android:id="@+id/nav_about"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="About"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/about_round"/>

</LinearLayout>

您的主要活动:

public class MainActivity extends AppCompatActivity {

private ViewGroup abotuUsVG;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    abotuUsVG = (ViewGroup) findViewById(R.id.nav_about);
    abotuUsVG.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // if you want to open in new activity
            // Todo: change YourAboutActivity.class with your real About activity name
            Intent intent = new Intent(MainActivity.this, YourAboutActivity.class);
            startActivity(intent);
        }
    });
}