![](/img/trans.png)
[英]How to show or set datepicker dialog for a textview that is inside a fragment in Android?
[英]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
。
我一定錯過了一些我不知道的東西。
任何幫助將不勝感激。
您正在創建一個新的LinearLayout
而不是引用 xml 文件中的那個。 將您的showPrefilledData
function 更改為,
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();
}
您從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 以編程方式添加視圖,但您需要做更多的事情才能使其工作。
如果您通過構造函數創建視圖(例如:TextView textView = new TextView();),則需要在新構建的視圖上調用 setLayoutParams,傳入父視圖的 LayoutParams 內部 ZA2F1 的實例 ZA2F8EABC26BB1DDC212ED4F8EBC26BB1構造子到父視圖。
例如,假設您的 LinearLayout 的 id 為 R.id.main_layout,您的 onCreate() function 中可能有以下代碼:
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,稍后當您需要它時,您可以將其可見性更改為 VISIBLE。 當可見性設置為 GONE 時,在可見性變為可見之前它不會占用任何空間。
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) {
}
dialog.setContentView(R.layout.cust_data_layout);
final LinearLayout linearLayout = new LinearLayout(context);
哈哈,看起來您的對話框內容視圖與您的 LinearLayout 無關。 讓我們在兩者中使用相似的視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.