2017-08-03 106 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