问题
I want to have a common bottom menu bar through out the applications with every page. I have designed the bottom bar but i am confused with onClick event of the menu icons. whether i have to write the code for onClick listener with every activity class to make bar visible and working in every page or if there is any other way i can create a common bottom bar that lies with every page without writing the code of menu in every activity class.
I tried to create through a creating a base class and extending it in other child classes, as stated by dave.c in the post Android creating Bottom Bar Menu but it didnt work for me. Please suggest. Thanks.
回答1:
Simple example (as dave.c suggested):
public class BaseActivity extends Activity {
public void onClickButton1(View view) {
Toast toast = Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT);
toast.show();
}
public void onClickButton2(View view) {
Intent i = new Intent(this, MyFirstActivity.class);
startActivity(i);
}
public void onClickButton3(View view) {
Intent i = new Intent(this, MySecondActivity.class);
startActivity(i);
}
}
Your MyFirstActivity will look like:
public class MyFirstActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_first_activity);
}
}
Your MySecondActivity activity:
public class MySecondActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_second_activity);
}
}
In my_first_activity.xml layout you include:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="My first activity" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<include android:layout_width="fill_parent" android:layout_height="wrap_content"
layout="@layout/bottom_bar" />
</LinearLayout>
In my_second_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="My second activity" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<include android:layout_width="fill_parent" android:layout_height="wrap_content"
layout="@layout/bottom_bar" />
</LinearLayout>
In bottom_bar you define buttons with onClick handlers:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Button1" android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="onClickButton1"/>
<Button android:text="Button2" android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="onClickButton2"/>
<Button android:text="Button3" android:id="@+id/button3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="onClickButton3"/>
</LinearLayout>
You might run into problems using this design. For example when you want to use ListView in some of your activities and you want to subclass ListActivity (TabActivity is another example) it will be not possible.
Another way is to subclass Activity and define one common handler that handles onClick events. In this case you will need to define onClick handlers in each activity and call corresponding common handler's methods.
Yet another way is to use TabHost and TabActivity.
来源:https://stackoverflow.com/questions/6054903/android-bottom-bar-menu-onclick-actions