我有一个在所有布局公共头的应用程序。我想,只要用户点击的ID为 btn_home
ImageView的,应用程序将返回到一个特定的活动,我的主来说吧。
什么是做到这一点的最好方法是什么?
我知道我可以定义的onClick(视图v)
的每个活动,但也许有更好的方法来做到这一点。即使尽一切活动会有一些(通过遗产)等具有的onClick(视图v)
定义听起来很糟糕。
header.xml
< RelativeLayout的...>
< RelativeLayout的机器人:ID =@ + ID / relativeLayout1...>
< ImageView的机器人:ID =@ + ID / logo_cats>< / ImageView的>
< ImageView的机器人:ID =@ + ID / btn_home...>< / ImageView的>
< / RelativeLayout的>
< / RelativeLayout的>
每一个布局
...
<包括布局=@布局/头机器人:ID =@ + ID /头
机器人:layout_height =WRAP_CONTENT机器人:layout_width =FILL_PARENT/>
...
解决方案
您可以在其中做出的自定义组件出你的头和界定的onClick()。例如,创建一个新类标题
将延长一个RelativeLayout的和膨胀的header.xml那里。然后,而不是<包括>
标签,你会使用< com.example.app.Header机器人:ID =@ + ID /头......
。没有code复制和头变得完全可重复使用的。
UPD:下面是一些code例子
header.xml:
<合并的xmlns:机器人=http://schemas.android.com/apk/res/android>
< ImageView的机器人:ID =@ + ID /标志... />
< TextView的机器人:ID =@ + /标记... />
<按钮机器人:ID =@ + ID /密码... />
< /合并>
activity_with_header.xml:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT...>
< com.example.app.Header机器人:ID =@ + ID /头... />
<! - 其他的观点 - >
< / RelativeLayout的>
Header.java:
公共类头扩展RelativeLayout的{
公共静态最终字符串变量= Header.class.getSimpleName();
保护ImageView的标志;
私人TextView的标签;
私人按钮loginButton;
公共头(上下文的背景下){
超(上下文);
}
公共头(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
公共头(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
}
公共无效initHeader(){
inflateHeader();
}
私人无效inflateHeader(){
LayoutInflater充气=(LayoutInflater)的getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.header,这一点);
标志=(ImageView的)findViewById(R.id.logo);
标签=(TextView中)findViewById(R.id.label);
loginButton =(按钮)findViewById(R.id.login);
}
ActivityWithHeader.java:
公共类ActivityWithHeader延伸活动{
私人查看mCreate;
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
的setContentView(R.layout.activity_with_header);
标题标题=(头)findViewById(R.id.header);
header.initHeader();
// 等等
}
}
在这个例子中,Header.initHeader()可以在里面头的构造运动,但一般这种方法提供了传递一些有用的听众一个很好的方式。希望这会有所帮助。
I have an application with a common Header in all layouts. I want that whenever the user clicks at the the ImageView with id btn_home
, the app will go back to a specific activity, my "Main" for instance.
What is the best way to do that?
I know that I can define the onClick(View v)
for every activity, but maybe there is a better way to do that. Even making every activity be some (via heritage) other that has the onClick(View v)
defined sounds bad.
header.xml
<RelativeLayout ...>
<RelativeLayout android:id="@+id/relativeLayout1" ...>
<ImageView android:id="@+id/logo_cats"></ImageView>
<ImageView android:id="@+id/btn_home" ...></ImageView>
</RelativeLayout>
</RelativeLayout>
every layout
...
<include layout="@layout/header" android:id="@+id/header"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
...
解决方案
You can make a custom component out of your header and define 'onClick()' in it. For example, make a new class Header
that would extend a RelativeLayout and inflate your header.xml there. Then, instead of <include>
tag you would use <com.example.app.Header android:id="@+id/header" ...
. No code duplication and the header becomes totally reusable.
UPD: Here's some code examples
header.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/logo" .../>
<TextView android:id="@+id/label" .../>
<Button android:id="@+id/login" .../>
</merge>
activity_with_header.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" ...>
<com.example.app.Header android:id="@+id/header" .../>
<!-- Other views -->
</RelativeLayout>
Header.java:
public class Header extends RelativeLayout {
public static final String TAG = Header.class.getSimpleName();
protected ImageView logo;
private TextView label;
private Button loginButton;
public Header(Context context) {
super(context);
}
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Header(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void initHeader() {
inflateHeader();
}
private void inflateHeader() {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.header, this);
logo = (ImageView) findViewById(R.id.logo);
label = (TextView) findViewById(R.id.label);
loginButton = (Button) findViewById(R.id.login);
}
ActivityWithHeader.java:
public class ActivityWithHeader extends Activity {
private View mCreate;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_with_header);
Header header = (Header) findViewById(R.id.header);
header.initHeader();
// and so on
}
}
In this example, Header.initHeader() can be moved inside Header's constructor, but generally this method provides a nice way to pass on some useful listeners. Hope this will help.