1

我想做一個簡單的支票簿應用程序,其MainActivity存儲事務列表。我希望屏幕頂部和底部的TextView分別顯示帳戶餘額和添加新交易的選項。我想要在該滾動之間的交易列表。我能夠實現一個ListView並添加一個頁眉和頁腳視圖,但是如果事務列表超出了屏幕的大小,頁眉和頁腳就可以滾動屏幕。如何在LinearLayout中實現ListView?

有沒有什麼辦法在線性佈局中定位ListView,或凍結頁眉/頁腳以保持屏幕?

這是到目前爲止我的XML文件:

<TextView 
    android:id="@+id/header_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/default_header_string"> 
</TextView> 

<ListView 
    android:id="@+id/transaction_list_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</ListView> 

<TextView 
    android:id="@+id/footer_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/add_transaction_string"> 
</TextView> 

這裏是我的onCreate,其中有沒有語法錯誤,但我無法點擊footerview增加一個交易:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_checkbook); 

    ListView list = (ListView) findViewById(R.id.transaction_list_view); 

    // Create a new Adapter 
    mAdapter = new TransactionAdapter(list.getContext()); 

    // Inflate footerView and headerView 
    LayoutInflater inflater = getLayoutInflater(); 
    TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null); 
    TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null); 

    // Set listener for footerView 
    footerView.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class); 
      startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST); 
     } 
    }); 

    list.setAdapter(mAdapter); 
} 
+0

你能說我,你爲什麼取決於LinearLayout中。使用RelativeLayout可以輕鬆完成。 –

+0

嗯,我試過只使用一個ListView,因爲你可以添加頁腳和頁眉視圖,但隨着列表長度的增加,這些視圖會從屏幕上滾動出來,我試圖找到一種方法將它們保留在屏幕上。我希望用戶總是看到帳戶餘額,並始終看到添加新交易的選項。 – AdamMc331

+0

是的,我的朋友可以用RelativeLayout輕鬆實現LinearLayout。 –

回答

2

使用下面的代碼。這將滿足您的要求。我試過這個,爲我工作。 具有以下屬性的相對佈局。相對佈局比使用權重方法的線性佈局要好。

<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:orientation="vertical" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_alignParentTop="true" 
    android:gravity="center_horizontal" 
    android:text="ListView Heading" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_alignParentBottom="true" 
    android:gravity="center_horizontal" 
    android:text="ListView Footer" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textView1" 
    android:layout_above="@id/textView2" 

    ></ListView> 

的UI會喜歡這個

enter image description here

+0

謝謝,那就設定了我想要的確切佈局! – AdamMc331

+0

請做出投票,並接受這個答案,如果你使用這個.. – AndEngine

+0

我沒有足夠的代表upvote呢。我一定會回來,並在我這樣做時這樣做。 – AdamMc331

0

我發現一個possible solution爲您的問題從一個類似的職位。希望這可以幫助你。

+0

感謝您的鏈接!這與我正在嘗試做的事情類似,這對於我將來可以實施的佈局來說絕對是一個想法。 – AdamMc331

0

對於您正試圖完成凍結頁眉/頁腳。這將是更容易使用相對佈局位置,然後在頁眉/頁腳有你的ListView中間

<RelativeLayout ...> 
<TextView 
    android:id="@+id/header_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:gravity="center" 
    android:text="@string/default_header_string"> 
</TextView> 

<ListView 
    android:id="@+id/transaction_list_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/header_view" 
    android:layout_above="@+id/footer_view"> 
</ListView> 

<TextView 
    android:id="@+id/footer_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="center" 
    android:text="@string/add_transaction_string"> 
</TextView> 
</RelativeLayout> 

可以使用的LinearLayout這項任務。但我不推薦它,因爲它有點「哈克」。

+0

謝謝!你說得對,RelativeLayout在我想要的方面效果更好。 – AdamMc331

1

是的,你可以在linearlayout中使用權重和layout_weight來做到這一點,也可以使用RelativeLayout創建這種類型的視圖。

1)在LinearLayout中,只需將weightsum="1"添加到您的線性佈局,並將layout_weight="0.2"添加到您的每個頁眉和頁腳,並將layout_weight="0.6"添加到您的列表視圖。

2)的RelativeLayout到您的報頭和alignParentBottom到頁腳並設置列表視圖添加alignParentTop到layout_below="@+id/header"layout_above="2+id/footer"

+0

佈局重量是個好主意。我認爲在這種特殊情況下,layout_above和layout_below方法效果最好,因爲它可以讓我玩弄文本大小等,而不用關心自己是否搞亂了其他對象的大小。感謝您的輸入! – AdamMc331

0
  1. 優惠數組中的所有元素:實施例: - (weatherArray)

  2. 循環通過所有的元素: -

實施例: -

mainLayout = ((LinearLayout)refreshObj.get("mainLayout")); 
    mainLayout.removeAllViews(); 

for (int i = 0; i < weatherArray.length(); i++) { 

View childView = getLayoutInflater().inflate(R.layout.weather_row4_item, mainLayout,false); 

TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus); 

todayTempStatus.setText(""); 

} 
  1. 這是不使用列表視圖,我們將填充lienarlayout使用子圖的實例。
1

試試這個方法,希望這會幫助你解決你的問題。 而不是使用頁眉/頁腳只是把下面的代碼在你的XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/header_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/default_header_string"> 
    </TextView> 

    <ListView 
     android:id="@+id/transaction_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </ListView> 

    <TextView 
     android:id="@+id/footer_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/add_transaction_string"> 
    </TextView> 
</LinearLayout> 
+0

這是一個好主意,根據你的回答以及對方共享的相對佈局,我實現了一些非常相似的東西。謝謝! – AdamMc331