简体   繁体   English

Android:使用ListActivity在同一活动上创建文本字段显示

[英]android: creating a textfield display on the same activity with a ListActivity

I have an activity that pulls a String Array from xml and displays a ListActivity (the class extends ListActivity) and I'd like to know if it is possible to also display a textfield or textView below the list? 我有一个活动,它从xml中提取一个字符串数组并显示一个ListActivity(该类扩展了ListActivity),我想知道是否有可能在列表下方显示一个textfield或textView吗?

If so, what method should I research to do this? 如果是这样,我应该研究哪种方法来做到这一点? Have code samples? 有代码样本吗?

Thanks! 谢谢!

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

} }

What you want is a vertical LinearLayout that contains a ListView and a TextView 您想要的是一个垂直的LinearLayout ,其中包含一个ListView和一个TextView

There is an example of what you want to achieve in the android reference: 在android参考中有一个您想要实现的示例:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. ListActivity具有默认布局,该布局由位于屏幕中央的单个全屏列表组成。 However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). 但是,如果需要,可以通过使用onCreate()中的setContentView()设置自己的视图布局来自定义屏幕布局。 To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code) 为此,您自己的视图必须包含ID为“ @android:id / list”(或列表,如果在代码中,则为List)的ListView对象

http://developer.android.com/reference/android/app/ListActivity.html 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM