如何在android中的listview下添加footbar按钮

时间:2013-10-31 09:48:10

标签: android listview button tabbar

我在listview下面的标尺中寻找标签类按钮,但这不会发生。在这里,我向您展示两个布局,其中一个有Tab Bar,另一个有ListView。我不确定在哪个布局我应该添加按钮以在下面的脚栏中显示以下是我的列表视图代码和快照:

Tabbar.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>

</TabHost>

Listitemselection.xml

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

    <include
        android:id="@+id/list_item_section_text"
        layout="@android:layout/preference_category" />

</LinearLayout>

Listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingRight="?android:attr/scrollbarSize" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginLeft="0dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_weight="0">

        <TextView
            android:id="@+id/list_item_entry_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/list_item_entry_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/list_item_entry_title"
            android:layout_below="@id/list_item_entry_title"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />
    </RelativeLayout>
</LinearLayout>

ListView.java

public class TestListView extends ListActivity {
    /** Called when the activity is first created. */

     ArrayList<Item> items = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    items.add(new SectionItem("Category 1"));
    items.add(new EntryItem("Item 1", "This is item 1.1"));
    items.add(new EntryItem("Item 2", "This is item 1.2"));
    items.add(new EntryItem("Item 3", "This is item 1.3"));


    items.add(new SectionItem("Category 2"));
    items.add(new EntryItem("Item 4", "This is item 2.1"));
    items.add(new EntryItem("Item 5", "This is item 2.2"));
    items.add(new EntryItem("Item 6", "This is item 2.3"));
    items.add(new EntryItem("Item 7", "This is item 2.4"));

    items.add(new SectionItem("Category 3"));
    items.add(new EntryItem("Item 8", "This is item 3.1"));
    items.add(new EntryItem("Item 9", "This is item 3.2"));
    items.add(new EntryItem("Item 10", "This is item 3.3"));
    items.add(new EntryItem("Item 11", "This is item 3.4"));
    items.add(new EntryItem("Item 12", "This is item 3.5"));

    EntryAdapter adapter = new EntryAdapter(this, items);


        EntryAdapter adapter = new EntryAdapter(this, items);

        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        if(!items.get(position).isSection()) {

            EntryItem item = (EntryItem)items.get(position);
            Toast.makeText(this, "You clicked " + item.title , Toast.LENGTH_SHORT).show();

        }

        super.onListItemClick(l, v, position, id);
    }
}

Tabbar.java

public class Support extends TabActivity {

    //private ImageView mSpinnerImage = null;
    //private Animation mAnimation = null;
    private Button button = null;
    private TextView synctitle = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            /*Titlebar + Button*/
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.support);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
            button  = (Button) findViewById(R.id.syncbutton);
            //mSpinnerImage = (ImageView) findViewById(R.id.spinner_view);
            //mAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_animation);
            //synctitle  = (TextView) findViewById(R.id.synctitle);
            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                     // TODO Auto-generated method stub
                     //button.setVisibility(View.INVISIBLE);
                     //mSpinnerImage.setVisibility(View.VISIBLE);
                     //synctitle.setVisibility(View.VISIBLE);
                     //mSpinnerImage.setAnimation(mAnimation);
                     //mSpinnerImage.startAnimation(mAnimation);
                }
            });
            /*Titlebar + Button ends*/

            /*Tabs*/
            Bundle bundle = getIntent().getExtras();
            TabHost tabHost = getTabHost();
            TabHost.TabSpec spec;
            Intent intent;

            intent = new Intent().setClass(this, TestListView.class);
            spec = tabHost  .newTabSpec("some_things")
                            .setIndicator("Info")
                            .setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, TestListView.class);
            spec = tabHost  .newTabSpec("top_things")
                            .setIndicator("Log")
                            .setContent(intent);
            tabHost.addTab(spec);


            //tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 85;
            //tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 85;
            /*Tabs ends*/
    }
}

2 个答案:

答案 0 :(得分:2)

要创建页脚按钮,请先创建一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Button" />

</RelativeLayout>

现在你的活动的OnCreate方法写下这个

LayoutInflater inflater = LayoutInflater.from(this);

View v = inflater.inflate(R.layout.button, null);

Button footer = (Button) v.findViewById(R.id.footer);

lv.addFooterView(v);  // use getListView() instead of lv if you are using ListActivity

更新

请勿使用ListActivity而是使用Activity

首先创建一个xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Button"
        android:layout_alignParentBottom="true" />

 </RelativeLayout>

现在onCreate扩展Activity并写下行

setContentView(R.layout.listxml); 

在super.onCreate(bundle)之后;

答案 1 :(得分:1)

试试这个         

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/LinearLayout2"
        android:background="#E7EFFA"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/txtLesson"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#E7EFFA"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/txtAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.30"
            android:background="#E7EFFA"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ff0000" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:background="#E7EFFA"
        android:gravity="center_vertical|center_horizontal" >

        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buy_button"
            android:minHeight="55dip"
            android:minWidth="55dip"
            android:paddingBottom="5dip"
            android:paddingTop="10dip"
            android:text="@string/PayButton"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </LinearLayout>

    <ListView
        android:id="@+id/lessonListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
         android:layout_above="@+id/bbar"
          android:layout_below="@+id/linearLayout1"
        android:background="#E7EFFA" >

    </ListView>

</RelativeLayout>