多个布局的菜单视图

时间:2013-10-09 14:29:41

标签: java android layout view

我创建了一个带有菜单视图的布局,该布局将显示在其他布局中,显示一些按钮。为了定义这些按钮及其动作,我正在创建一个独立的类来定义所有这些按钮,这样我就不必在所有活动中反复定义所有这些按钮。

为此,首先我创建了menu_view布局:

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

注意:这里不是所有的按钮,我只有2个想法。

在此之后,我创建了MenuView类,在其中我定义了所有这些按钮以及它们的作用:

public class MenuView extends RelativeLayout {

private final LayoutInflater inflater;

public MenuView(Context context, AttributeSet attrs) {
    super(context, attrs);

    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);
}

 private final OnClickListener launch_nav = new OnClickListener() {
    @Override
    public void onClick(View v) {
        getContext().startActivity(new Intent(getContext(), Navigation.class));
    }
};

**¡¡¡THROUBLE!!!**
private final OnClickListener launch_phone = new OnClickListener() {
    @Override
    public void onClick(View v) {
        public void launch_phone (String number) {
            String numberToDial = "tel:"+number;
        }
        getContext().startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numberToDial)));
    }
};

private final OnClickListener launch_web = new OnClickListener() {
    @Override
    public void onClick(View v) {
        getContext().startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.es")));
    }
};

**¡¡¡THROUBLE!!!**
private final OnClickListener goBack = new OnClickListener() {
    @Override
    public void onClick(View v) {
        getContext().startActivity(new Intent(getContext(), NO.class));
    }
};
}

这是我有点失落的地方。我必须定义2个动作:

1-电话拨号器   2- goBack密钥

电话拨号器我不知道如何在这里定义它。当我第一次创建这个应用程序时,我已经用其他方式定义了,它工作得很好..但是现在,有很多活动,我必须这样做,这里显示错误,因为这不是办法它

使用后退按钮,我需要的是定义当按下此按钮时,它必须返回活动,但同样,在这种类中我不知道如何定义它。

1 个答案:

答案 0 :(得分:0)

听起来好像快速浏览一下您的应用程序,您将代码分离到最初存在的Activity之外,现在要构造它们以便它们位于一个单独的类对象中以供引用。在构造类时,您已经在上下文中传递,因此您应该能够保存传入的上下文并将其用作goBack调用的启动。否则,您在视图上下文中调用goBack,后退按钮与Activity关联(理想情况下,您在创建它时将其传递给此类),因此您希望将上下文传递到视图中。

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));
    }
};
}