【问题标题】:Adding textview inside a linearlayout of a dialog fragment inside a class in android在android中的类内的对话框片段的线性布局中添加textview
【发布时间】:2021-08-07 01:09:05
【问题描述】:

我有一个带有scrollview 的布局。在里面,我有一个LinearLayoutButton

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">

<LinearLayout
    android:id="@+id/ll_details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="10dp">




    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="OK" />
</LinearLayout>

我正在使用dialogfragment 来查看此布局。另外我正在尝试动态添加textview。下面是我的课

public class SecondLevelAdapter extends BaseExpandableListAdapter {
    private void showPreFilledData(String string) {
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.cust_data_layout);
    dialog.setTitle("Customer Info");

    final LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.findViewById(R.id.ll_details);

    TextView textView1 = new TextView(context);
    textView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    textView1.setGravity(Gravity.CENTER);
    textView1.setText("programmatically created TextView1");
    linearLayout.addView(textView1);


    
    Button ok;

    ok = (Button) dialog.findViewById(R.id.ok);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    Window window = dialog.getWindow();
    window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
    dialog.show();

}
}

但是当我启动应用程序时,我看不到textview

我一定错过了一些我不知道的东西。

任何帮助将不胜感激。

【问题讨论】:

  • 您能否确保您的 textView1 颜色不是白色。因为您的布局背景是白色的。所以如果文本颜色为白色,则变得不可见。

标签: android android-layout textview android-dialogfragment android-dynamic-shortcuts


【解决方案1】:

您正在创建一个新的LinearLayout,而不是引用 xml 文件中的那个。将您的 showPrefilledData 函数更改为,

private void showPreFilledData(String string) {
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.cust_data_layout);
        dialog.setTitle("Customer Info");

        LinearLayout linearLayout = dialog.findViewById(R.id.ll_details);

        TextView textView1 = new TextView(context);
        textView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        textView1.setGravity(Gravity.CENTER);
        textView1.setText("programmatically created TextView1");
        linearLayout.addView(textView1);



        Button ok;

        ok = (Button) dialog.findViewById(R.id.ok);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        Window window = dialog.getWindow();
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
        dialog.show();

    }

【讨论】:

    【解决方案2】:

    您从xml 文件中获取LinearLayout 的方式不正确。

    final LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.findViewById(R.id.ll_details);
    

    据我了解,这会创建一个新的 LinearLayout 并尝试在其下查找 ID 为 ll_details 的子视图。我想你想要

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_details);
    

    然后,将视图添加到 LinearLayout 应该可以工作。

    【讨论】:

    • 我收到error: cannot find symbol findViewById
    • 已编辑:我正在尝试在类中动态添加textview
    • 哦,你只能在activity类里面使用我的解决方案。在这种情况下,您能否将活动类中的LinearLayout 传递到您添加它的地方?
    • 另一种解决方案是通过活动类(在活动类中使用this),并使用activity.findViewById() + 里面的id 得到LinearLayout
    • 其实这个类是在第三点调用的意思是说例如Fragment-&gt;Class1-&gt;Class2
    【解决方案3】:

    您需要调用 addView 以编程方式添加视图,但您还需要做更多工作才能使其正常工作。

    如果通过构造函数创建视图(例如:TextView textView = new TextView();),则需要在新构造的视图上调用 setLayoutParams,传入父视图的 LayoutParams 内部类的实例,然后再将新建的子视图添加到父视图。

    例如,假设您的 LinearLayout 的 ID 为 R.id.main_layout,您的 onCreate() 函数中可能包含以下代码:

     LinearLayout myLayout = findViewById(R.id.main_layout);
    
     TextView textView = new TextView(this);
     textView.setLayoutParams(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.MATCH_PARENT,
                                     LinearLayout.LayoutParams.MATCH_PARENT));
    
     myLayout.addView(textView);
    

    确保设置 LayoutParams 很重要。每个视图至少需要一个 layout_width 和一个 layout_height 参数。获得正确的内部类也很重要。

    另外,我不确定您的用例,但您也可以在 xml 本身中添加一个视图,其可见性为 GONE,稍后当您需要时,您可以将其可见性更改为 VISIBLE。当可见性设置为 GONE 时,它不会占用任何空间,直到可见性变为 VISIBLE。

    【讨论】:

      【解决方案4】:

      Forgot Everything 删除了冗余代码。使用此代码并设置您的自定义对话框 UI 布局以替换为 R.layout.my_dilog_ui 并将其放入您的代码中。

       try {
              LayoutInflater factory = LayoutInflater.from(context);
              View views = factory.inflate(R.layout.my_dilog_ui, null);
      
              AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
              AlertDialog alertDialog = alertDialogBuilder.create();
      
              alertDialogBuilder.setView(views);
      
              TextView tv_title = (TextView) views.findViewById(R.id.title);
              TextView title = views.findViewById(R.id.my_title);
              Button close = views.findViewById(R.id.close);
      
              tv_title.setText("Are you sure do you want to return your Order?");
              title.setText("Return request can not be revoked ");
      
              close.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      alertDialog.dismiss();
                  }
              });
      
          
              alertDialog.setView(views);
         
              alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
              alertDialog.show();
          } catch (Exception e) {
      
          }
      

      【讨论】:

        【解决方案5】:

        dialog.setContentView(R.layout.cust_data_layout);

        final LinearLayout linearLayout = new LinearLayout(context);

        哈哈,看起来您的对话框内容视图与您的 LinearLayout 无关。让我们在两者中使用相似的视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多