在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();
}