2

同じように水平方向にスクロール可能なボタンの列を追加したいと思います。

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

(toolbar.xml)私のアプリケーションのすべてのアクティビティの下部に。すべてのアクティビティで各ボタンのクリックリスナーを指定する必要はなく、すべてを1つの場所で実行して、毎回コントロールをインポートできるようにしたいと思います。私は私が次のようなことをすることができると思います

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

しかし、それでは何ですか?実際に表示するにはどうすればよいですか?オーバーライドする必要がある他の方法はありますか?これを行うためのより良い/より適切な方法はありますか?

4

1 に答える 1

5

私のアプリ BBC Newsのカスタム コントロールProgressViewと、それを使用する 1 つのレイアウトを見てください。

http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/ProgressView.java

http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/res/layout/progress_view.xml

http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/res/layout/main.xml

于 2011-04-22T15:07:52.693 に答える