如何为所有底栏图标添加Click Listener

时间:2014-12-15 11:43:38

标签: android android-layout android-xml

您好,在我的应用中,我为所有屏幕添加了一个常见的底栏。

我做的是,

首先,我在底部栏中设计了三个图标的xml布局(footer.xml),然后我在所有活动中使用了底栏。现在,在所有活动中都可以看到底栏。

现在我的问题是,我想在底栏中的所有三个图标中添加一个共同的onClicklistener()。我试图创建一个扩展RelativeLayout的类文件,但它不能正常工作。任何人都可以告诉我如何将onClickListener()添加到底栏中的所有三个图标。

我的xml(footer_for_all.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:layout_weight=".15"
android:background="#12A5F4"
android:gravity="bottom"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/footerLocationIcon"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:layout_weight=".14"
    android:src="@drawable/ic_location_icon" />

<ImageView
    android:id="@+id/footerMessageIcon"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight=".14"
    android:src="@drawable/ic_message_icon" />

<ImageView
    android:id="@+id/footerRightMenuIcon"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_weight="0.14"
    android:src="@drawable/ic_right_icon" >
</ImageView>

</LinearLayout>

然后我在所有使用的活动中包含了页脚

<LinearLayout
    android:id="@+id/down"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true" >

    <include
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        layout="@layout/footer_for_all" />
</LinearLayout>

我的Java文件是

public class FooterForDriver extends Activity{

ImageView mapLocationBtn, chatBtn, rightMenuBtn;

private LayoutInflater inflater;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.footer_for_all);

    mapLocationBtn=(ImageView) findViewById(R.id.footerLocationIcon);
    chatBtn=(ImageView) findViewById(R.id.footerMessageIcon);
    rightMenuBtn=(ImageView) findViewById(R.id.footerRightMenuIcon);

    mapLocationBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), "Button Clicked", Toast.LENGTH_LONG).show();
            Intent openMap = new Intent(getApplicationContext(), CustomerProfileForDriver.class);
            startActivity(openMap);
        }
    });

    chatBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_LONG).show();
        }
    });

    rightMenuBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_LONG).show();
        }
    });
}
}

3 个答案:

答案 0 :(得分:1)

我在其中一个应用中做了类似的事情。我需要多个图像按钮,只需一次点击监听。

尝试这样的事情:

mapLocationBtn.setOnClickListener(getApplicationContext());
chatBtn.setOnClickListener(getApplicationContext());
rightMenuBtn.setOnClickListener(getApplicationContext());
public void onClick(View v) {


        switch(v.getId())
        {
          case R.id.mapLocationBtn:
            // code here
            break;
          case R.idchatBtn:
            // code here
            break;
          case R.id.rightMenuBtn:
            // code here
            break;
        }
    } 

您可能需要调整对象,但其中的要点就在那里。

答案 1 :(得分:0)

试试这个......

        OnClickListener commonlistener=new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    };
    mapLocationBtn.setOnClickListener(commonlistener);
    chatBtn.setOnClickListener(commonlistener);
    rightMenuBtn.setOnClickListener(commonlistener);

答案 2 :(得分:-1)

  //tell me why this layout no perfectly showing on any differnt screen size //phone....

  <?xml version="1.0" encoding="utf-8"?>

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

        android:layout_width="match_parent"
        enter code here
        android:layout_height="match_parent"

        android:orientation="horizontal"

        android:id="@+id/linearMain"

        tools:context="com.cycloneapps.skyphotoeditor.MainActivity">

    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"
    >

        <ScrollView

        android:layout_width="90dp"

        android:layout_height="match_parent"

            android:id="@+id/scroll">

        <LinearLayout
            android:layout_width="90dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:background="@drawable/btn_bg" >
    <ImageButton
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:id="@+id/frame1"
        android:background="@drawable/thumb_1"
        android:layout_marginTop="10dp"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame2"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_2"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame3"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_3"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame4"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_4"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame5"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_5"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame6"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_6"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame7"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_7"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame8"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_8"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:id="@+id/frame9"
                android:background="@drawable/thumb_9"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:id="@+id/frame10"
                android:background="@drawable/thumb_10"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:id="@+id/frame11"
                android:background="@drawable/thumb_11"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame12"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_12"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:id="@+id/frame13"
                android:background="@drawable/thumb_13"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame14"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_14"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame15"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_15"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:id="@+id/frame16"
                android:background="@drawable/thumb_16"/>
            <ImageButton
                android:layout_width="70dp"
                android:id="@+id/frame17"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_17"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:id="@+id/frame18"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_18"/>
            <ImageButton
                android:layout_width="70dp"
                android:id="@+id/frame19"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/thumb_19"/>
            <ImageButton
                android:layout_width="70dp"
                android:layout_height="0dp"
                android:layout_marginTop="10dp"
                android:id="@+id/frame20"
                android:background="@drawable/thumb_20"/>
        </LinearLayout>

    </ScrollView>
        <RelativeLayout
            android:id="@+id/editorMainFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
         android:layout_marginLeft="90dp">
            <ImageView
                android:id="@+id/showimage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="matrix"
                />

            <ImageView
                android:id="@+id/frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <LinearLayout
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:background="@drawable/btn_bg"
                android:orientation="vertical"
                android:layout_marginLeft="550dp"
         android:gravity="center"
                >

                <ImageButton
                    android:id="@+id/stick"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:background="@drawable/btn_smile" />

                <ImageButton
                    android:id="@+id/effect"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:background="@drawable/btn_effect" />
            <ImageButton
                android:id="@+id/textbtn"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:background="@drawable/text" />

                <ImageButton
                    android:id="@+id/save"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_alignParentLeft="true"
                    android:layout_gravity="center"
                    android:layout_alignParentStart="true"
                    android:background="@drawable/btn_save" />

                <ImageButton
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:id="@+id/share"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:background="@drawable/btn_share" />




     </LinearLayout>
    </RelativeLayout>
    </RelativeLayout>