Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中的类中的对话框片段的线性布局中添加textview_Android_Android Layout_Textview_Android Dialogfragment_Android Dynamic Shortcuts - Fatal编程技术网

在android中的类中的对话框片段的线性布局中添加textview

在android中的类中的对话框片段的线性布局中添加textview,android,android-layout,textview,android-dialogfragment,android-dynamic-shortcuts,Android,Android Layout,Textview,Android Dialogfragment,Android Dynamic Shortcuts,我有一个带有滚动视图的布局。在它里面,我有一个线性布局和按钮 <?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=&q

我有一个带有
滚动视图的布局。在它里面,我有一个
线性布局
按钮

<?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>
但当我启动应用程序时,我无法看到
textview

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


非常感谢您的帮助。

您从
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);

然后,将视图添加到该
线性布局中应该可以工作。

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

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

例如,假设LinearLayout具有id R.id.main\u布局,则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非常重要。每个视图至少需要一个布局宽度和布局高度参数。此外,获得正确的内部类也很重要


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

忘记所有内容将删除冗余代码。使用此代码并将自定义对话框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) {

    }

您正在创建一个新的
线性布局
,而不是引用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();

    }

dialog.setContentView(R.layout.cust\u data\u布局)

final LinearLayout LinearLayout=新的LinearLayout(上下文)


哈哈,看起来你的对话框内容视图与你的线性布局无关。让我们在两者中使用类似的视图。

我得到了
错误:找不到symbol findViewById
编辑:我试图在类中动态添加
textview
。哦,您只能在activity类中使用我的解决方案。在这种情况下,能否将
LinearLayout
从活动类传递到添加它的位置?另一种解决方案是传递活动类(在活动类中使用
this
),并使用
activity.findViewById()
+内部id来获取
LinearLayout
,这个类在第三点被调用,意思是说,例如,
Fragment->Class1->Class2
你能确保你的textView1颜色不是白色吗。因为您的布局背景是白色的。所以,如果文本颜色是白色,则不可见。
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();

    }