【问题标题】:Not able to add Title bar for Listview无法为 Listview 添加标题栏
【发布时间】:2011-09-19 08:35:20
【问题描述】:

对我的屏幕的要求就像在顶部中间有一个标题栏,然后是列表视图,当我尝试这样做时,我无法像我在每个列表项中都有状态名称那样做。我只希望在顶部和一次。它的屏幕截图是

 <?xml version="1.0" encoding="utf-8"?>
    <TableLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
     android:layout_height="fill_parent"
        >
      <TableRow>
      <TextView 
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:background="#000000"
         android:text="Select State"
                
      ></TextView>
      </TableRow>
      <TableRow> 
      <TextView  android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          
           android:id="@+id/label"
          android:textSize="30px"></TextView>
    </TableRow>
    </TableLayout>

我尝试了很多方法这是我现在的布局

我的java代码是

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class new21 extends ListActivity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Andhra Pradesh", "Kerala","Tamilnadu","Karnataka" };
        // Use your own layout and point the adapter to the UI elements which
        // contains the label
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.new21,
                R.id.label, names));
    }

    @Override
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        if(keyword.equals("Andhra Pradesh"))
        {
            Intent ima45=new Intent(new21.this,new31.class);
            startActivity(ima45);
    
        }
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();
        
    }

}

谁能帮帮我。

【问题讨论】:

  • 请更新您的 new21.xml 整个

标签: android listview titlebar


【解决方案1】:

你可以像这样将Header添加到ListView,

View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);
setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice, names));

其中R.layout.header 是包含TextView 和文本"Select State" 的布局

更新:

public class ListViewProblemActivity extends ListActivity {

    ListView listView;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Andhra Pradesh", "Kerala","Tamilnadu","Karnataka" };
        // Use your own layout and point the adapter to the UI elements which
        // contains the label

        TextView tv = new TextView(getApplicationContext());
        tv.setText("Select State");

        listView = getListView();
        listView.addHeaderView(tv);
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));
    }

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked

        if(position != 0){
            System.out.println(position - 1);
            Object o = this.getListAdapter().getItem(position - 1);
            String keyword = o.toString();
            Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                    .show();
        }
    }
}

【讨论】:

    【解决方案2】:

    在您的活动中仅使用ListView,删除那里的标题。然后使用ListView.addHeaderViwe(); 将标题添加到Listview。此标题显示在第一项之前。 有关详细信息,请参阅documentaton

    【讨论】:

      【解决方案3】:

      你最好使用 ListView.addHeaderViwe();

      但不要忘记删除你所做的“扩展 ListActivity”,不需要,

      并从“new21.xml”中删除标题 在您的 xml 中,只需修复您想要在列表的每一行中显示的文本项,

      【讨论】:

        【解决方案4】:

        你完全错了。只需要一个带有一个 TextView(用于标题栏)和一个 ListView(用于显示列表视图)的 LinearLayout,仅此 2 个视图。

        <?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="fill_parent"    
             android:orientation="vertical">
        
             <TextView 
                 android:layout_height="wrap_content"
                 android:layout_width="fill_parent"
                 android:background="#000000"
                 android:text="Select State">
              </TextView>
        
              <ListView 
                 android:id="@+id/lviewState"
                 android:layout_height="wrap_content"
                 android:layout_width="match_parent">
              </ListView>
        
        </LinearLayout>
        

        现在,按照您的代码显示 ListView。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-11
          • 2011-10-21
          相关资源
          最近更新 更多