I am displaying a listView on my ListViewActivity. And I would like to add a button to the top of the LisView. I have tried this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
setContentView(R.layout.listview_singlegrey);
LinearLayout linear = (LinearLayout) findViewById(R.id.list_comment);
Button btAddComment = new Button(this);
btAddComment.setText("Añadir comentario");
linear.addView(btAddComment);
setContentView(linear);
//....
}
The file listview_singlegrey.xml is as follows:
<?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:id="@+id/list_comment"
android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/fondomain">
<ListView android:id="@+id/android:list" android:listSelector="@android:color/transparent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:divider="@layout/linedivider" android:dividerHeight="10px"
android:cacheColorHint="#0000" android:paddingTop="10dp"
/>
</Line开发者_Python百科arLayout>
But I cant see the button. Any ideas why?
If you want to add button to the top of your listview, you must add that to zero'th index
linear.addView(btAddComment, 0);
Now your listview is in index 1.
Get rid of
setContentView(linear);
Also, try adding the button in XML first and make sure it shows up. You may need to fiddle with layout_height
and layout_weight
parameters so the ListView doesn't push it offscreen. Once you know the right parameters, set them at runtime.
Have a look to this method addHeaderView which adds a fixed view to appear at the top of the list.
i guess you miss the LayoutParams for the button when you create it in code. Use Button.setLayoutParams(new LayoutParams(...)).
But i dont recommend this way. Why dont you just add the button above your listview in the XML? Then it stays fixed there. Like this:
<LinearLayout> <Button/> <ListView /> </LinearLayout>
If you want a "scrollable button", which scrolls with the listview, write a XML file with the button, inflate it via getLayoutInflater.inflate(R.layout. ..) and use ListView.addHeaderView().
What you need to use the addHeaderView of ListView
.
According to the documentation
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want. NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.
Do you want a header for listview? you can use header like this
listView.addHeaderView(view);
Here view may be any view component.
You should try setting the layout parameters before adding the button to the layout.
Something like this:
btAddComment.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
That should do the trick. If not, let me know.
精彩评论