2017-08-03 90 views
2

我已經看過這個問題here,但我仍然無法找出我做錯了什麼。 Logcat中沒有錯誤,並且確實有數據被傳遞給它。以下是我的設置:動態創建佈局不顯示

這些都發生在我放置在Android Studio中的手動放置元素下方。我有一個ScrollView。在ScrollView中,我有一個LinearLayout,parentLayout,它被傳遞給這個類。此方法應該添加另一個水平LinearLayout,layout,parentLayout。然後它應該添加一個TextView,titleDisplay和兩個按鈕到layout。到目前爲止,我只編程了layouttitleDisplay。我測試了它,沒有添加任何東西。所以在我編程其他兩個按鈕之前,我想知道我做錯了什麼。這裏的Java代碼:

public class FollowupOption { 

private String displayName; 
private JSONObject jsonInformation; 
private Context context; 
private LinearLayout parentLayout; 

private LinearLayout layout; 
private TextView titleDisplay; 
private Button deleteButton, editButton; 

public FollowupOption(String displayName, JSONObject jsonInformation, 
         Context context, LinearLayout parentLayout){ 
    this.displayName = displayName; 
    this.jsonInformation = jsonInformation; 
    this.context = context; 
    this.parentLayout = parentLayout; 
    buildLayout(); 
} 

private void buildLayout(){ 
    //Horizontal Linear Layout to hold everything 
    this.layout = new LinearLayout(context); 
    this.layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
    this.layout.setOrientation(LinearLayout.HORIZONTAL); 
    this.parentLayout.addView(this.layout); 
    //Text View Displaying title of followup option. 
    this.titleDisplay = new TextView(context); 
    try { 
     this.titleDisplay.setText(this.jsonInformation.getJSONObject("list").getString("title")); 
    } catch(JSONException e){ e.printStackTrace(); } 
    this.titleDisplay.setTextColor(0x8f142a); //Black 
    this.titleDisplay.setTextSize(18); 
    this.titleDisplay.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)); 
    this.layout.addView(this.titleDisplay); 
} 
} 

這裏是我的XML:

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

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="38dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/textView15" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="0.22" 
       android:gravity="center" 
       android:text="@string/followup_text" 
       android:textColor="@color/myRed" 
       android:textSize="18sp" /> 

      <Button 
       android:id="@+id/followup_add_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@android:color/transparent" 
       android:text="@string/plus" 
       android:textAlignment="center" 
       android:textColor="@android:color/holo_green_dark" 
       android:textSize="30sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="279dp" 
      android:paddingLeft="7dp" 
      android:paddingRight="7dp" 
      android:paddingTop="7dp"> 

      <LinearLayout 
       android:id="@+id/followup_parent" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

      </LinearLayout> 
     </ScrollView> 

     <Button 
      android:id="@+id/followup_new_button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/new_followup_text" /> 
    </LinearLayout> 
</merge> 

如果有人可以讓我知道我做錯了或調試這樣的事情,這將不勝感激的好方法。

+0

XML將幫助確保這不是佈局問題 – Milk

+0

我使用XML @Milk更新了問題 – SH7890

+0

您是否看到堆棧跟蹤?你可以嘗試用一些靜態文本替換'this.titleDisplay.setText(...)'嗎? – Milk

回答

1

你是否在某些視圖中添加this.layout視圖?

UPD:問題在於您的文本顏色。考慮使用Color類從其十六進制值或該類的常量中獲取顏色。

+0

不是這行'this.parentLayout.addView(this.layout);'那樣做? – SH7890

+0

什麼是'this.parentLayout'?我可能是'parentLayout'沒有添加到視圖中。 – Milk

+1

嗯,它的確如此。你能提供這個類的完整代碼嗎?不知道爲什麼這不起作用 –

0

您的XML視圖(沒有添加動態佈局)佔用了整個屏幕嗎?

將在按鈕下方添加新的LinearLayout視圖。如果按鈕位於屏幕底部,則佈局將從屏幕添加,因此不可見。

您應該將新佈局添加到滾動視圖中。

+0

我相信,視圖添加在屏幕中間的滾動視圖 –

+0

嗯,我認爲新的LinearLayout將被添加到'parentLayout'(這是xml中的followup_parent)。我會嘗試將您的建議添加到ScrollView中。 – SH7890

+0

還沒有顯示。 – SH7890