2012-10-28 102 views
4

我在一个按钮和一个列表视图里面使用线性布局.. list-view在运行时动态地添加它的项目.. 所以那时按钮落后列表视图和不可见以及列表视图适合整个布局。android-想把按钮放在listview上面

我想提出的按钮列表视图之上,它也必须保持装列表视图后提前

感谢

我的.xml文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#000000"> 


    <Button 
     android:id="@+id/btn_searchNewDevices" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:layout_gravity="left" 
     android:background="#000000" 
     android:textColor="#FFFFFF" 
     android:text="Search New Devices" 
     /> 

    <ListView 
     android:id="@+id/lst_add_newDevices" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="horizontal" > 

    </ListView> 
</LinearLayout> 

enter image description here

这里是搜索新设备按钮。

对蓝牙该按钮设备搜索点击添加到列表视图 enter image description here

所以这件事在我的应用程序发生..

回答

0

尝试在ListView的高度,使用android:layout_weight="1"你要确保你的按钮将永远在屏幕上看到。

你应该尝试:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:orientation="vertical" > 
    <Button 
     android:id="@+id/btn_searchNewDevices" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_marginTop="5dip" 
     android:background="#000000" 
     android:text="Search New Devices" 
     android:textColor="#FFFFFF" /> 
    <ListView 
     android:id="@+id/lst_add_newDevices" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:scrollbars="horizontal" > 
    </ListView> 
</LinearLayout> 

或者您可以使用ListView的addHeaderView()方法。这是一个tutorial

+0

直到它不工作.. –

1

我想即使这个works.Give一试

编辑...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/relative" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/btn_searchNewDevices" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_marginTop="5dip" 

     android:text="Search New Devices" 
     android:textColor="#000000" /> 

    <ListView 
     android:id="@+id/lst_add_newDevices" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/btn_searchNewDevices" 
     android:scrollbars="horizontal"> 
    </ListView> 

</RelativeLayout> 

和活动如下。

public class MainActivity extends Activity implements OnClickListener { 
    ArrayAdapter<String> adapter; 
    private Button mBTNSearch; 
    private static ListView mLV_list; 
    ArrayList<String> devicesList; 
    SparseBooleanArray checked; 
    RelativeLayout layout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mBTNSearch = (Button) findViewById(R.id.btn_searchNewDevices); 
     devicesList = new ArrayList<String>(); 

     mLV_list = (ListView) findViewById(R.id.lst_add_newDevices); 
     adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, devicesList); 
     mBTNSearch.setOnClickListener(this); 
     mLV_list.setAdapter(adapter); 
    } 

    public void onClick(View v) { 

     adapter.add("new Device"); 
     adapter.notifyDataSetChanged(); 
    } 

} 

输出捕捉如下

enter image description here

+0

给其他解决方案..它给出相同的输出作为我的 –

+0

检查编辑。 –

+0

仍然不起作用 –

0

尝试为您的布局验证码。

<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:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
    </ListView> 
</LinearLayout> 

+0

它给出和我一样的输出。给其他解决方案 –

0

尝试把你的ListView滚动型内。

<Button 
    android:id="@+id/btn_searchNewDevices" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:layout_marginTop="5dip" 
    android:background="#000000" 
    android:text="Search New Devices" 
    android:textColor="#FFFFFF" /> 

<ScrollView 
    android:id="@+id/scroll" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_margin="7dip" 
    android:scrollbars="none" > 

    <ListView 
     android:id="@+id/lst_add_newDevices" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="horizontal" > 
    </ListView> 
</ScrollView> 

它可能会奏效。我没有测试过,所以不确定。