在 android 中的 class 中的对话框片段的线性布局中添加文本视图
Adding textview inside a linearlayout of a dialog fragment inside a class in android
我有一个 scrollview
的布局。在里面,我有一个 LinearLayout
和 Button
.
<?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
。下面是我的 class
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
。
我一定是漏掉了一些我不知道的东西。
非常感谢任何帮助。
您从 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
应该可以。
您需要调用 addView 以编程方式添加视图,但您还需要做更多的工作才能使其正常工作。
如果通过构造函数创建View(eg:TextView textView = new TextView();),则需要在新建的view上调用setLayoutParams,传入父view的LayoutParams inner实例class,在将新建的子视图添加到父视图之前。
例如,假设您的 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 参数。获得正确的内部 class 也很重要。
此外,我不确定您的用例,但您也可以在 xml 本身中添加一个视图,其可见性为 GONE,稍后当您需要它时,您可以将其可见性更改为可见的。当可见性设置为 GONE 时,在可见性变为 VISIBLE 之前不需要任何 space。
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) {
}
您正在创建一个新的 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();
}
dialog.setContentView(R.layout.cust_data_layout);
final LinearLayout linearLayout = new LinearLayout(context);
哈哈,看来您的对话框内容视图与您的 LinearLayout 无关。让我们在两者中使用相似的视图。
我有一个 scrollview
的布局。在里面,我有一个 LinearLayout
和 Button
.
<?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
。下面是我的 class
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
。
我一定是漏掉了一些我不知道的东西。
非常感谢任何帮助。
您从 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
应该可以。
您需要调用 addView 以编程方式添加视图,但您还需要做更多的工作才能使其正常工作。
如果通过构造函数创建View(eg:TextView textView = new TextView();),则需要在新建的view上调用setLayoutParams,传入父view的LayoutParams inner实例class,在将新建的子视图添加到父视图之前。
例如,假设您的 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 参数。获得正确的内部 class 也很重要。
此外,我不确定您的用例,但您也可以在 xml 本身中添加一个视图,其可见性为 GONE,稍后当您需要它时,您可以将其可见性更改为可见的。当可见性设置为 GONE 时,在可见性变为 VISIBLE 之前不需要任何 space。
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) {
}
您正在创建一个新的 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();
}
dialog.setContentView(R.layout.cust_data_layout);
final LinearLayout linearLayout = new LinearLayout(context);
哈哈,看来您的对话框内容视图与您的 LinearLayout 无关。让我们在两者中使用相似的视图。