2011-04-21 43 views
0

我有一个活动,它从xml中拉出一个字符串数组并显示一个ListActivity(该类扩展ListActivity),我想知道是否也可以在列表下面显示一个textfield或textView?android:在与ListActivity相同的活动上创建文本字段显示

如果是这样,我应该研究什么方法来做到这一点?有代码示例?

谢谢!

CODE:

public class txchl extends ListActivity { 
/** Called when the activity is first created. */ 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    //setContentView(R.layout.main); 
    String[] rmenu = getResources().getStringArray(R.array.root_menu); 
    if (rmenu != null) { 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, rmenu)); 
    } 
    TextView tv = new TextView(this); 
    tv.setText("Hello"); 
    setContentView(tv); 
} 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    // 
    if (position == 4 || position == 5) { 
     Intent myIntent = new Intent(v.getContext(), Activity2.class); 
     myIntent.putExtra("com.activity.Key", position); 
     startActivity(myIntent); 
    } else { 
     Intent myIntent = new Intent(v.getContext(), txchl_hb.class); 
     myIntent.putExtra("com.activity.Key", position); 
     //myIntent.putExtra("com.activity.Dir", directives[position]); 
     startActivity(myIntent); 
    } 
} 

}

回答

0

你想要的是一个垂直LinearLayout包含ListViewTextView

还有就是你想在Android参考达到什么样的一个例子:

ListActivity有一个预设t由屏幕中央的单个全屏列表组成的布局。但是,如果您愿意,可以通过在onCreate()中使用setContentView()设置您自己的视图布局来自定义屏幕布局。要做到这一点,你自己的观点必须包含id为一个ListView对象“@android:ID /列表”(或列表,如果它在代码)

http://developer.android.com/reference/android/app/ListActivity.html

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp"> 

    <ListView android:id="@id/android:list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#00FF00" 
       android:layout_weight="1" 
       android:drawSelectorOnTop="false"/> 

    <TextView android:id="@id/android:empty" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#FF0000" 
       android:text="No data"/> 
</LinearLayout> 
+0

我已经在上市这两个对象我的layout.xml并使用ListActivity,然后调用setContentView,但只有列表可见。我将把代码添加到我的OP中。 – bzo311 2011-04-21 22:22:58

+0

如果ListView为空,上面的代码只显示TextView。不知道这是我们在这里寻找的。您可能想尝试将该TextView的android:id更改为“@ id/android:empty”以外的内容。 – BigFwoosh 2011-04-21 22:27:48

+0

编辑ID后显示文本。我现在需要了解它的显示位置和格式。谢谢! – bzo311 2011-04-21 22:35:49

相关问题