リストビューの上部に2つのボタンが必要です。リストビューの下をスクロールする場合は、ボタンを一番上に置いておきたいです。私はstackoverflowで多くのソリューションを検索しましたが、私の場合に正確なソリューションを見つけることができませんでした。
私のレイアウトxml(mainlayout.xml):
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/latest_news_button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
作成されたグラフィックレイアウトは、まさに私が望んでいたものです。
Javaコードをレイアウトにバインドできない場合に備えて、このレイアウトを使用するためのコードを次に示します。
public class MainActivity extends ListActivity{
Button latestButton;
Button trackedButton;
ListView lv = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
latestButton = (Button)findViewById(R.id.button1);
latestButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Latest Button!", Toast.LENGTH_LONG).show();
}
});
trackedButton = (Button)findViewById(R.id.button2);
trackedButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Tracked Button!", Toast.LENGTH_LONG).show();
}
});
this.lv = getListView();
String[] values = new String[]{"asdfa","qwerqwer","banbn"};
lv.setAdapter(new ArrayAdapter<String>(BilTrackerMainActivity.this, android.R.layout.simple_expandable_list_item_1, values));
}
ただし、このアクティビティが他のアクティビティから呼び出された場合、残念ながらアプリは閉じられました。LogCatによると:
コンテンツには、id属性が「android.R.id.list」であるListViewが必要です。
lv = (ListView)findViewById(R.id.mylist);
しかし、私はの代わりに追加してみthis.lv = getListView();
ます。この問題は解決できませんでした。私は知ってListView#addHeaderView
いますが、この種のレイアウトを使用することに関する私の特定の問題を解決したいと思います。