Android视图,带编辑框和按钮作为标题

时间:2012-03-15 11:40:50

标签: android

我有一个我以正常方式实施的列表视图。我想在列表视图的顶部有一个编辑框和一个按钮,而不是列表视图中的每个项目。有人可以为我提供用于此目的的示例代码

3 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/listado_ly_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/listado_tb_filtro"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.45" android:layout_gravity="center_vertical">

        </EditText>

        <Button
            android:id="@+id/listado_iv_search"
            android:layout_width="48dp"
            android:layout_height="48dp" android:background="@android:drawable/ic_menu_search"/>


    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:minHeight="200sp"
        android:orientation="horizontal" >


            <ListView
                android:id="@+id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:choiceMode="singleChoice"
                android:drawSelectorOnTop="false"
                android:minHeight="200sp" >
            </ListView>
    </LinearLayout>

答案 1 :(得分:1)

试试这个..

<?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" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/edtxt"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:inputType="none" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:layout_marginRight="3dp"
            android:layout_marginTop="2dp"
            android:text="button"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/list"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
    </LinearLayout>

</LinearLayout>

Java文件将如下所示

public class example extends Activity {
private ListView listView;
static String[] names;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         listView = (ListView) findViewById(R.id.phlist);
Button btn = (Button) findViewById(R.id.btn);
EditText ed = (EditText)findViewById(R.id.edtxt);
names = new String[] { "name1", "name2", "name3", "name4" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, android.R.id.text1, values); 

            lv.setAdapter(adapter); 

btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
//write Ur Code for Button Clik

}
});

}
}

参考此链接。可能有用.. enter link description here

答案 2 :(得分:1)

根据我的解释,您可以尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF000000" >
    <LinearLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/linearLayout1"
         android:orientation="horizontal"
        >
     <EditText 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/et1"
         />
     <Button 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/bt1"
         android:text="ClickMe"
         />
     </LinearLayout>
     <ListView
        android:id="@+id/list"
        android:layout_below="@id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#000000" />


</RelativeLayout>

您可能需要根据需要自定义某些参数。希望这会有所帮助。

编辑1:相应的Java类

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AtoZActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView lv=(ListView) findViewById(R.id.list);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);

            // Assign adapter to ListView
            lv.setAdapter(adapter);
    }
}

在清单中

  <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AtoZActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>