1

リストビューの上部に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いますが、この種のレイアウトを使用することに関する私の特定の問題を解決したいと思います。

4

5 に答える 5

4

リストビューIDを次のように変更するだけです android:id="@android:id/list"

于 2012-05-01T10:28:15.733 に答える
1

RelativeLayoutを使用します。layout_toRightOfを使用して最初のボタンの右側に2番目のボタンを配置し、layout_belowを使用してボタン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="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_toRightOf="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/tracked_news_button" />

    <ListView
        android:id="@+id/mylist"
        android:layout_below="@id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>
于 2012-05-01T10:31:29.553 に答える
0

[リストビュー]と[ボタンを使用したLinearLayout]を使用してFrameLayoutを作成します。

onCreate()内でこのようなことを一度実行します

LinearLayout myButtons = ...;
myButtons.bringToFront();

したがって、ContentViewには2つの子を持つFrameLayoutがあり、そのうちの1つはフォアグラウンドにあります。

それで全部です。お役に立てば幸いです))

于 2012-05-01T10:34:44.370 に答える
0

これがあなたが探しているxmlです

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


 <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    style="@android:style/ButtonBar">

 <Button
    android:id="@+id/btn_AddMore"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/newItem"
    android:textColor="#FF394952"/>
 </LinearLayout>

 <ListView
    android:id="@+id/game_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_weight="1.0" />
</LinearLayout>

また、このアクティビティを設計するときは、リストアクティビティを直接使用しないでください。アクティビティを直接使用し、リストビューでアダプタを設定してください。

 ListView lv = (ListView) findViewById(R.id.game_list);
    ArrayAdapter<String> listItem = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, ALBUM_NAME);

    lv.setAdapter(listItem);
    lv.setTextFilterEnabled(true);
于 2012-05-01T10:26:34.403 に答える
0

そのために以下のxmlファイルを使用してください

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/mLlayout1" >
        <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="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mLlayout1" >
    </ListView>

</RelativeLayout>
于 2012-05-01T10:28:59.330 に答える