主屏幕上的图像是调用活动的按钮操作栏上的按钮是否可以调用与主屏幕上的按钮相同的活动..我应该制作什么样的按钮才能调用活动....我在菜单布局中声明了操作栏,如果它有助于确定我如何制作操作栏..问题是要使用的按钮,因此它可以调用活动..
这是我的菜单 xml 文件
<item
android:id="@+id/open"
android:showAsAction="ifRoom|withText"
android:title="open"
android:icon="@drawable/ic_action_open"
android:menuCategory="secondary"></item>
<item
android:id="@+id/save"
android:showAsAction="ifRoom|withText"
android:title="Save"
android:icon="@drawable/ic_action_save"></item>
<item
android:id="@+id/close"
android:showAsAction="ifRoom|withText"
android:title="Close"
android:icon="@drawable/ic_action_close"></item>
<item
android:id="@+id/history"
android:showAsAction="ifRoom|withText"
android:title="History"
android:icon="@drawable/ic_action_history"></item>
<item
android:id="@+id/settings"
android:showAsAction="ifRoom|withText"
android:title="Settings"
android:icon="@drawable/ic_action_settings"></item>
</menu>
我的 .java 文件
package name.brucephillips.actionbarexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class ActionBarExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sample (View v)
{
Intent intent = new Intent(getApplicationContext(), Sample.class);
startActivity(intent);
}
public void sample1(View v)
{
Intent intent = new Intent(getApplicationContext(), Sample1.class);
startActivity(intent);
}
public void sample2(View v)
{
Intent intent = new Intent(getApplicationContext(), Sample2.class);
startActivity(intent);
}
public void sample3(View v) {
Intent intent = new Intent(getApplicationContext(), Sample3.class);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sample:
startActivity(new Intent(getApplicationContext(), Sample.class));
break;
case R.id.sample1:
startActivity(new Intent(getApplicationContext(), Sample1.class));
break;
case R.id.sample2:
startActivity(new Intent(getApplicationContext(), Sample2.class));
break;
case R.id.sample3:
startActivity(new Intent(getApplicationContext(), Sample3.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
这是我的主要 xml
android:orientation="vertical">
<ImageView
android:id="@+id/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample"
android:src="@drawable/ic_action_close" />
<ImageView
android:id="@+id/sample1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample1"`enter code here`
android:src="@drawable/ic_action_history" />
<ImageView
android:id="@+id/sample2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample2"
android:src="@drawable/ic_action_open" />
<ImageButton
android:id="@+id/sample3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample3"
android:src="@drawable/ic_action_save" />
</LinearLayout>