当前位置: 首页 > 知识库问答 >
问题:

动态创建的布局未显示

柳墨一
2023-03-14

我已经在这里看了这个问题,但是我仍然找不到我做错了什么。Logcat中没有错误,并且肯定有数据被传递给它进行制作。这是我的设置:

这一切都发生在我在Android Studio中手动放置的元素下面。我有一个滚动视图。在这个滚动视图中,我有一个LinearLayout,即parentLayout,它被传递给这个类。此方法应添加另一个水平线性布局,布局,父布局。然后应该添加一个文本视图、标题显示和两个按钮到布局。到目前为止,我只编写了布局和标题显示。我测试了它,没有添加任何内容。因此,在我对其他两个按钮进行编程之前,我想知道我做错了什么。下面是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>

如果有人能让我知道我做错了什么,或者有一个很好的方法来调试这样的东西,那将不胜感激。


共有2个答案

闾丘德业
2023-03-14

您的XML视图(没有添加动态布局)是否占据了整个屏幕?

新的线性布局视图将添加到按钮下方。如果按钮位于屏幕底部,布局将添加到屏幕外,因此不可见。

您应该将新布局添加到滚动视图中。

微生青青
2023-03-14
匿名用户

您正在添加此。布局查看到某个视图?

UPD:问题在于您的文本颜色。考虑使用Color类从其十六进制值或该类的常量中获取颜色。

 类似资料:
  • 问题内容: 我试图使用此代码从我的主要活动中调用第二个活动 作为我的OrderSummaryActivity: 但是布局不会在此活动中显示,只是空白页。这是布局的xml: 有什么提示吗? 问题答案: 尝试将 OrderSummaryActivity.class 更改为:

  • 问题内容: 我是Android开发的新手,已经开始创建自己的UI。我看到您可以动态创建它,如下所示(Dynamic Layouts ): 但我也看到netbeans有一个文件 Resources- > layout-> main.xml 。因此,您可以为UI创建XML布局(声明XML布局): 所以我的问题是应该使用哪个?在Android开发中,对动态布局和XML布局有何建议和优缺点? 问题答案:

  • 我试图以编程方式创建一个LinearLayout,但由于某种原因,它没有显示,我在Logcat或Run终端中没有错误。 以下是我的Java代码: 和我的XML: 我试图实现的是有3线性布局创建根据数组长度,因为我将通过一些TextViews给他们以后 我试图遵循我找到的答案,用多个视图以编程/动态方式创建LinearLayout,但仍然看不到在模拟器上创建的LinearLayout。 下面是它的显

  • 12.2.2.创建XML布局 小部件的外观布局很简单。留意我们在这里重用了TimelineActivity中用到的row.xml文件,用以表示消息的显示。另外再给它加一个小标题,在主屏幕上更醒目些。 例 12.2. res/layout/yamba_widget.xml <?xml version="1.0" encoding="utf-8"?> <!-- #1 --> <LinearLayout

  • 问题内容: 给定这样的HTML文件: 如何创建一个布局状态,以一个标题模板填充“标题”,用一个脚注模板填充页脚,然后允许子状态填充空的ui视图? 我想空的ui-view也可以命名为ui-view =“ main”。 问题答案: 一种方法是建立全局“根”状态。因此, 每个 州都是孩子。像root.pages,root.pages.edit,root.settings等。然后,您可以提供页眉和页脚的默

  • 问题内容: 我在XML中定义了以下布局: 如何使用LayoutInflater来获取ListView和ProgressBar并在代码中分配它? 问题答案: 通过这种方式: