Java 多个布局的菜单视图
我已经创建了一个带有菜单视图的布局,它将在其他布局中显示一些按钮。为了定义这些按钮及其动作,我创建了一个独立类来定义所有按钮,这样我就避免了在所有活动中一次又一次地定义所有按钮 为此,首先我创建了菜单视图布局:Java 多个布局的菜单视图,java,android,layout,view,Java,Android,Layout,View,我已经创建了一个带有菜单视图的布局,它将在其他布局中显示一些按钮。为了定义这些按钮及其动作,我创建了一个独立类来定义所有按钮,这样我就避免了在所有活动中一次又一次地定义所有按钮 为此,首先我创建了菜单视图布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_he
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- Button columns -->
<RelativeLayout
android:id="@+id/border_left"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:background="#000000" >
<ImageView
android:id="@+id/radioButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:layout_marginTop="60dp"
android:layout_marginLeft="10dp"
android:onClick="launch_radio"
android:src="@drawable/radio_button" />
<ImageView
android:id="@+id/musicButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:layout_marginTop="200dp"
android:layout_marginLeft="10dp"
android:onClick="launch_media"
android:src="@drawable/music_button" />
</RelativeLayout>
</RelativeLayout>
这是我有点迷路的地方。我必须定义两个动作:
1-电话拨号器
2-goBack键
电话拨号器我不知道如何定义它就在这里。当我第一次创建这个应用程序时,我已经用另一种方式定义了它,它工作得很好。。但是现在,有很多活动,我必须这样做,这里它显示了一个错误,因为这不是这样做的方式
对于“后退”按钮,我需要定义的是,当按下此按钮时,它必须返回活动,但再次,在这种类型的类中,我不知道如何定义它。通过对应用程序的快速概述,听起来好像您正在将代码隔离在活动之外,它最初存在于活动中,现在将这些代码进行结构化,以便它们位于要引用的单独类对象中。在构造类时,您已经传入了一个上下文,因此您应该能够保存传入的上下文,并将其用作goBack调用的启动。否则,您将在视图上下文上调用一个goBack,并且back按钮与一个活动相关联(理想情况下,这是您在创建该类时传递到该类中的内容),因此您希望将上下文传递到视图中
private activityContext;
public MenuView(Context context, AttributeSet attrs) {
super(context, attrs);
activityContext = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.menu_view, this, true);
((ImageView)this.findViewById(R.id.navButton)).setOnClickListener(launch_nav);
((ImageView)this.findViewById(R.id.phoneButton)).setOnClickListener(launch_phone);
((ImageView)this.findViewById(R.id.webButton)).setOnClickListener(launch_web);
((ImageView)this.findViewById(R.id.backButton)).setOnClickListener(goBack);
}
**¡¡¡THROUBLE!!!**
private final OnClickListener goBack = new OnClickListener() {
@Override
public void onClick(View v) {
activityContext.startActivity(new Intent(activityContext, NO.class));
}
};
}
你不是用碎片做的吗?嗯,这可能是另一种方式。我只是android世界的初学者,我还有很多东西要学。不管怎样,我问的问题有什么解决办法吗?我有很多工作是这样做的,我想让它这样工作。如果我有时间的话,我会看看如何处理碎片。@A.S.刚刚读了链接,不能告诉我们吗,因为我正在开发android 2.3。另一种解决方案?在2.3版本上,支持库也是可能的,我将说明使用该代码的任何示例。这段代码不是完全由我开发的,我从android开始,我做这件事的方式是阅读在互联网上实现这一点的方法理解你所说的,不管怎样,这项功能还没有完成,因为正如你所看到的,我称之为
NO.class
,这不是一个真正的类,只是在示例中定义它的一种方式,所以函数不完整,这就是我想要的
private activityContext;
public MenuView(Context context, AttributeSet attrs) {
super(context, attrs);
activityContext = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.menu_view, this, true);
((ImageView)this.findViewById(R.id.navButton)).setOnClickListener(launch_nav);
((ImageView)this.findViewById(R.id.phoneButton)).setOnClickListener(launch_phone);
((ImageView)this.findViewById(R.id.webButton)).setOnClickListener(launch_web);
((ImageView)this.findViewById(R.id.backButton)).setOnClickListener(goBack);
}
**¡¡¡THROUBLE!!!**
private final OnClickListener goBack = new OnClickListener() {
@Override
public void onClick(View v) {
activityContext.startActivity(new Intent(activityContext, NO.class));
}
};
}