一个基本活动拿着按钮 [英] A base Activity holding a button

查看:82
本文介绍了一个基本活动拿着按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想在一个独特的位置做的一切活动同样的功能添加一个按钮,这样反而增加了相同的code到所有的活动,我觉得做一个基础的活动,在这里它是

In my application I want to add a button at a unique position doing the same function in all activities, so instead of adding the same code to all activities, I thought of making a base activity, and here it is.

mport android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
 public abstract class DefaultActivity extends Activity{

ImageButton b;
@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.default_activity);
    b = (ImageButton)findViewById(R.id.imageButton1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "ana fel base activity", Toast.LENGTH_LONG).show();
        }
    });
}

}

但犯规出现在其他活动,虽然我做了其他的活动延长按钮 DefaultActivity 。那么,有没有办法做到这一点?非常感谢。

But the button doesnt appear in the other activities although I made other activities extend DefaultActivity. So is there a way to make this happen? Thanks alot.

P.S:我不想用动作条

P.S: I don't want to use the actionBar.

推荐答案

如果你的其他活动,称之为的setContentView(...)与其他布局,那么这些布局会被证明。如果这些布局没有你的的ImageButton ,那么你不会有一个的ImageButton 在屏幕上(布局赢得了 T鸟巢通过调用的setContentView()在两个活动和它的超类实现)。一种解决方案是使用<包括> 标签中的其他布局和有其他布局文件,就像这样的ImageButton:

If your other activities call setContentView(...) with other layouts, then those layouts are going to be shown. If those layouts don't have your ImageButton, then you won't have an ImageButton on screen (layouts won't "nest" by calling setContentView() in both the Activity and it's superclass implementation). One solution is to use the <include> tag in your other layouts and have another layout file with just the ImageButton like so:

RES /布局/ image_button.xml

<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
/>

RES /布局/ activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- more views ... -->

    <include android:layout="@layout/image_button" />
</LinearLayout>

这篇关于一个基本活动拿着按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆