从XML创建自定义视图 [英] Creating custom view from xml
问题描述
我想加按钮的同一个水平滚动的一行像这样
I would like to add the same horizontal scrollable row of buttons like so
<HorizontalScrollView [...]>
<LinearLayout [...] android:orientation="horizontal">
<Button android:id="@+id/btn1" [..] />
<Button [..] />
[...]
</LinearLayout>
</HorizontalScrollView>
(toolbar.xml)的每一个活动,我的应用程序的底部。而不是在每一个活动指定为每个按钮的点击听众,我希望能够做到这一切在一个地方,然后每次只需导入控制。我想我可以这样做
(toolbar.xml) to the bottom of every activity in my application. Rather than have to specify the click listeners for each button in every single activity, I'd like to be able to do all of that in one place and then just import the control each time. I figure I can do something like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.ButtonBar android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:layout_below="@+id/pagecontent" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/pagecontent">
<!-- the rest of each activity's xml -->
</LinearLayout>
要包括屏幕上的按钮栏,然后像做
to include the button bar on the screen, and then do something like
package com.example;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
public class ButtonBar extends HorizontalScrollView implements OnClickListener
{
public ButtonBar(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.toolbar, null);
Button btn1 = (Button) view.findViewById(R.id.button1);
btn1.setOnClickListener(this);
// and so on for the rest of the buttons
addView(View);
}
@Override
public void onClick(View v)
{
Intent intent = null;
if (v.getId() == R.id.btn1)
{
intent = new Intent(getContext(), FirstScreen.class);
}
else if (v.getId() == R.id.btn2)
{
intent = new Intent(getContext(), SecondScreen.class);
}
// and so on
if (intent != null) getContext().startActivity(intent);
}
}
但然后呢?我如何真正得到它显示?还有没有其他的方法,我应该重写?是否有这样做的更好/更合适的方式?
but then what? How do I actually get it to display? Are there other methods I should be overriding? Is there a better / more appropriate way of doing this?
推荐答案
以使用它来看看我的应用程序BBC新闻自定义控件 ProgressView
,以及一个布局
Take a look at a custom control ProgressView
in my app BBC News, and one layout that uses it.
<一个href=\"http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/ProgressView.java\">http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/ProgressView.java
<一个href=\"http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/res/layout/progress_view.xml\">http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/res/layout/progress_view.xml
<一个href=\"http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/res/layout/main.xml\">http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/res/layout/main.xml
这篇关于从XML创建自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!