我已經看過這個問題here,但我仍然無法找出我做錯了什麼。 Logcat中沒有錯誤,並且確實有數據被傳遞給它。以下是我的設置:動態創建佈局不顯示
這些都發生在我放置在Android Studio中的手動放置元素下方。我有一個ScrollView。在ScrollView中,我有一個LinearLayout,parentLayout
,它被傳遞給這個類。此方法應該添加另一個水平LinearLayout,layout
,parentLayout
。然後它應該添加一個TextView,titleDisplay
和兩個按鈕到layout
。到目前爲止,我只編程了layout
和titleDisplay
。我測試了它,沒有添加任何東西。所以在我編程其他兩個按鈕之前,我想知道我做錯了什麼。這裏的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>
如果有人可以讓我知道我做錯了或調試這樣的事情,這將不勝感激的好方法。
XML將幫助確保這不是佈局問題 – Milk
我使用XML @Milk更新了問題 – SH7890
您是否看到堆棧跟蹤?你可以嘗試用一些靜態文本替換'this.titleDisplay.setText(...)'嗎? – Milk