2013-07-12 134 views
1

我需要創建一個全屏幕機器人活動編程如下面的圖像中:how screen should look like的Android佈局按預期不顯示

兩個按鈕應保持在屏幕的底部。 虛擬內容將由不同的組件(textviews,單選按鈕,複選框等)組成,並且將被動態填充。

這是我的代碼至今:

 //Main Layout 
    FrameLayout lLayout = new FrameLayout(this); 
    lLayout.setBackgroundColor(Color.parseColor("#0099cc")); 
    lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 
    //Navigation layout 
    LinearLayout l = new LinearLayout(this, null, R.style.ButtonBar); 
    LayoutParams bottomLayout = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); 
    l.setLayoutParams(bottomLayout); 
    l.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL); 
    l.setBackgroundColor(Color.parseColor("#66000000")); 
    l.setOrientation(LinearLayout.HORIZONTAL); 


    LayoutParams buttLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    //previous section button 
    previousButton = new Button(this); 
    previousButton.setLayoutParams(buttLayout); 
    previousButton.setText("Previous section"); 
    previousButton.setOnClickListener(this); 
    l.addView(previousButton); 
    //next section button 
    Button nextButton = new Button(this); 
    nextButton.setLayoutParams(buttLayout); 
    nextButton.setText("Next section"); 
    nextButton.setOnClickListener(this); 
    l.addView(nextButton); 

    //add components 
    TextView tView = new TextView(this); 
    tView.setText("Dummy text"); 
    tView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
    lLayout.addView(tView); 
    lLayout.addView(l); 
    setContentView(lLayout); 

這裏是代碼產生: enter image description here

Tthere幾點按預期不起作用: 1.按鈕是在頂部而不是底部。 2.按鈕不散開很好 3. TextView的我加入作爲試驗中所示的按鈕的後面。我將在屏幕上顯示許多不同的小部件,並期望它們大於一個屏幕。我想有一個滾動選項,但所有這些小部件不應該在應該在屏幕底部的兩個按鈕後面看到。

+1

你爲什麼不使用Android的XML編輯器創建的佈局? – Blackbelt

+0

建議ü用XML –

+0

去由於XML問題的性質,不能使用... – Jasko

回答

2

以下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" > 

    <LinearLayout 
     android:id="@+id/dynamiclayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/navigationlayout" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/navigationlayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:text="Previous Section" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="Next Section" /> 
    </RelativeLayout> 

</RelativeLayout> 

現在編程膨脹dynamiclayout,並添加所有的動態視圖進去。

+0

正如我所說的,因爲我需要這一切都被編程開發我不能使用XML。我知道如何通過XML來完成,但不幸的是,硬編碼是我需要的。 – Jasko

+0

您提到的虛擬內容是需要動態添加的內容嗎?底部的按鈕始終保持固定? –

+0

半真實。是虛擬內容會動態生成,但我不能使用XML來創建按鈕。我將擁有以此屏幕爲基礎的動態活動數量,並且在某些情況下,按鈕上的文本會發生變化,有時會顯示其中一個或另一個。 – Jasko

2

你的根視圖是一個FrameLayout,其意在只有一個孩子查看。它也經常用於創建重疊視圖,因爲所有的FrameLayout的孩子都將被放在屏幕上的相同位置。

替換您FrameLayoutRelativeLayout。確保您更新LayoutParams參考文獻以使用RelativeLayout.LayoutParams。您還需要設置導航的LinearLayout與父視圖的底部對齊,像這樣:

RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
lps.addRule(RelativeLayout.LayoutParams.ALIGN_PARENT_BOTTOM, true); 

雖然我真的建議使用XML。它會讓你的生活變得更簡單。

+0

我想通了如何使用XML做到這一點。但是,您的評論對其他內容非常有用,所以非常感謝。 – Jasko