2013-07-04 78 views
3

相對佈局的底部我對Android非常新。我正在創建一個包含可點擊文本的應用程序。 所有可點擊的文本都存在於一個數組中,然後該數組與ListView關聯,並顯示在RelativeLayout中。需要一個按鈕在Android

代碼是 -

this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.label, contact_name)); 
    ListView lv = getListView(); 

主要是相對佈局,標籤佈局中的文本框的id和CONTACT_NAME是去爲可點擊文本的數組。這一切工作正常。

最後,當數組非常大時,線性佈局變得可滾動。

現在我想限制這個列表佔用的區域佔總屏幕高度的80%或90%,並在底部預留一個按鈕,這將打開一個新的頁面/視圖。在相對佈局中包含按鈕是爲列表中的每個項目添加一個按鈕。更改相對佈局的高度正在改變列表中每個項目的高度。由此得出結論,數組中的每個項目都與整個相對佈局相關聯,並且相對佈局的數組來顯示所有項目。現在,如何通過將相對佈局列表限制在頂部屏幕的80%來在底部放置按鈕。

這是當前的XML文件的樣子的代碼

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btn" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:text="Next" 
     /> 

    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/btn"> 

     <TextView 
      android:id="@+id/label" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dip" 
      android:textSize="16sp" 
      android:textStyle="bold" > 
     </TextView> 
    </ScrollView> 
</RelativeLayout> 

結束輸出爲隱藏的實際內容下一個按鈕的陣列。

這是我的Java代碼

// Make the contact number parameter accessible to member functions of List View 
     final String[] f_contact_number = contact_number; 

     // Binding Array to ListAdapter 
     this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.label, contact_name)); 

     ListView lv = getListView(); 

     // listening to single list item on click 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

       Intent intent = new Intent(Intent.ACTION_CALL); 
       intent.setData(Uri.parse(f_contact_number[position])); 
       startActivity(intent); 

      } 
     }); 

Contact_number和contact_names兩個字符串數組,其上單擊CONTACT_NAME

Thanks 

回答

2

這是更簡單。使用RelativeLayout做叫號。如果你想做到這一點使用LinearLayout,您可以設置的意見layout_height0dp和使用layout_weight分發高度

+0

你能檢查一下代碼並告訴我我在做什麼錯?我嘗試了很多,但我得到的輸出是不希望的。 – user2550128

2

我認爲你應該做這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btn" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:text="Next" 
     /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/btn"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Scroll view content" 
      > 
     </TextView> 
    </ScrollView> 
</RelativeLayout> 

因此,按鈕將堅持父母的底部和剩餘的空間將被佔用ScrollView 希望這會有所幫助。

+0

我試過這個,但不知何故按鈕顯示爲一個列表。下一個按鈕的數量等於數組中元素的數量。根據需要在底部沒有單個下一步按鈕。數組中的實際文本不可見並且不可點擊。可能每個Next按鈕都躺在這些文本上並隱藏它們 – user2550128

+0

您可以編輯您的問題以顯示xml文件嗎? @ user2550128 –

+0

你的xml在我的模擬器中給出了正確的o/p。你在java文件中如何在scrollview中加載文本? @ user2550128 –

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/yourbtnid" 
    android:scrollbars="vertical" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="your text" > 
    </TextView> 
    </ScrollView> 

    <Button 
    android:id="@+id/yourbtnid" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_gravity="center_vertical" 
    android:text="Button" /> 

</RelativeLayout> 
+0

此代碼和Rapunzel建議的代碼基本相同。請看看創建的輸出。它有點像按鈕 按鈕 按鈕 按鈕垂直排列,實際元素不可見 – user2550128

0

我做了Anamika的建議,它像一個魅力。

我改了一個代碼,所以按鈕設置在中間,並沒有填滿所有的空間。

也許你的問題是在java代碼中,而不是在佈局代碼中。

你上班了嗎?

請讓我知道如果你還在掙扎,我有看看它,並幫助你解決。