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

标签 android listview button tabbar

我正在 ListView 下方的脚栏中寻找选项卡类型的按钮,但这没有发生。在这里,我向您展示了两种布局,一种是 Tab Bar,另一种是 ListView。我不确定我应该在哪个布局中添加按钮以将其放在 footbar 下面是我的 ListView 代码和快照:

标签栏.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>

列表项选择.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*/
    }
}

最佳答案

要创建页脚按钮首先创建一个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>

现在在你的 Activity 的 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 extends Activity 并写在下面几行

setContentView(R.layout.listxml); 

在 super.onCreate(bundle) 之后;

关于android - 如何在android中的listview下方添加footbar按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703079/

相关文章:

java - 将 fragment 中的 ListView 链接到不同的 Activity

listview - jQuery mobile listview - 无法将id添加到动态输入框

java - 单独添加按钮

java - 使用 fragment 更改 Honeycomb 上的布局

android - 我们可以在 Firebase Cloud Messaging 中创建多少主题?

android - TouchableOpacity 组件设置为 position absolute not clickable in android

java - 单击提交后填充 jframe

Android - 将下一个和上一个按钮添加到软键盘,该按钮在我的 Activity 中单击编辑文本时出现

java - 如何从 AsyncTask 的 postExecute 方法修改 ArrayAdapter 的数据

android - ImageViews 和按钮 Android 应用程序