从 xml 创建自定义视图

发布于 2024-11-02 18:11:49 字数 2352 浏览 0 评论 0原文

我想将相同的水平可滚动按钮行添加

<HorizontalScrollView [...]>
  <LinearLayout [...] android:orientation="horizontal">
    <Button android:id="@+id/btn1" [..] />
    <Button [..] />
     [...] 
  </LinearLayout>
</HorizontalScrollView>

到我的应用程序中每个活动的底部,如下所示(toolbar.xml)。我希望能够在一个地方完成所有这些操作,然后每次都导入控件,而不是必须为每个活动中的每个按钮指定单击侦听器。我想我可以做一些事情,

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

比如在屏幕上包含按钮栏,然后做一些类似的事情,

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

但是然后呢?我实际上如何让它显示?还有其他我应该重写的方法吗?有更好/更合适的方法吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文