2013-03-24 57 views
1

我正在開發一個項目,我需要動態地添加TextViewSpinner以及通過該程序。我能夠從我的程序成功動態地添加這兩件事。如何修復Android中Button的位置

下面是我用於此的XML佈局。

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

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/llayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="100px" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal|center" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="Save" /> 

    </LinearLayout> 

</ScrollView> 

Problem Statement:- 

因此,我希望是 - 所有TextViewSpinner應該得到所示,首先然後在最後Button應該得到什麼所示。

但是,當前發生了什麼Save Button顯示在頂部,然後所有其他TextView and Spinner得到顯示。

我怎樣才能確定,該按鈕來自屏幕的底部。

下面是你可以找出問題的截圖。我想在屏幕底部顯示保存按鈕。

enter image description here

回答

1

或者你也可以在代碼中創建一個按鈕,這樣,再添加全部後添加到您的佈局在紡織和像下面textviews,你必須從你的XML中刪除按鈕的聲明:

Button button = new Button(this) ///this is a context object 

    button.setWidth(100); 
    button.setText("Save"); 


    layout.addView(button); 

您可以動態創建佈局並添加按鈕,使L ayout然後將此佈局添加到超級佈局

或者您可以做的是在res/layout/savebutton_layout中聲明新的xml文件。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="horizontal" 
     android:id="@+id/lLayoutBT" 

     > 

     <Button 
      android:id="@+id/saveBT" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/save" /> 


    </LinearLayout> 

現在,在你的代碼,你將不得不尋找包含按鈕此佈局,然後將此佈局添加到您的主要佈局(就像你將你的按鈕)

例如

 LinearLayout buttonLayout =(LinearLayout) view.findViewById(R.id.lLayoutBT); 
    views.add(buttonLayout); 
+0

我試過這個,但在這種情況下。按鈕的大小隻是屏幕的寬度。我想讓Button完全顯示,因爲它在問題中的圖像中顯示。任何想法如何使這項工作? – ferhan 2013-03-24 23:52:24

+0

你需要一個佈局。我編輯了我的答案並添加了示例代碼以將按鈕添加到佈局。 – 2013-03-25 09:35:56

0

如果您改變LinearLayoutRelativeLayout然後添加這個您Button

<Button 
... 
android:layout_alignParentBottom="true"/> 

它應該給你想要的東西。我儘可能多地使用RelativeLayout而不是LinearLayout,因爲在大多數情況下,它更加靈活和高效。他們採取習慣於多一點的時間,但絕對值得花時間去理解和執行

編輯

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

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/llayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"/> 
     android:text="Save" /> 

</RelativeLayout> 

</ScrollView> 

類似的東西應該工作。你可能需要玩一點,才能得到你想要的東西,但這應該會讓你在那裏。

+0

感謝您的幫助。你能提供我在我的代碼中使用的XML嗎?正如我相信我在我的代碼中搞砸了一些東西。謝謝您的幫助。 – ferhan 2013-03-24 23:42:15

+0

編輯我的答案 – codeMagic 2013-03-24 23:55:19

0

如果你使用(而不是LinearLayout)一RelativeLayout你有一個像訪問屬性:

<Button 
android:id="@+id/button1" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
... 
</Button>