对话框未显示textview& ImageView的

时间:2014-01-13 09:13:35

标签: android textview android-dialog

我是android世界的新手,正在进行对话,我正在尝试创建一个文本视图,按钮和imageview,但它既没有显示错误,也没有显示textview或imageview。它显示一个按钮。这是图像:

dialouge

但我希望按以下方式显示我的对话

need as this

我的xml文件如下....

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context=".AddFriendsCustomActivity">

    <ImageView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/submit_dialog"
        android:layout_alignParentRight="true"
        android:layout_marginRight="37dp"
        android:contentDescription="@string/add_Friend"
        android:src="@drawable/add" />

    <Button
        android:id="@+id/submit_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="64dp"
        android:layout_toLeftOf="@+id/add"
        android:text="@string/submit" />

    <EditText
        android:id="@+id/writename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/add"
        android:layout_marginRight="17dp"

        android:layout_toLeftOf="@+id/add"
        android:ems="10" />

</RelativeLayout>

我在java文件中调用它如下

dialog = new Dialog(context);
                     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        View view = li.inflate(R.layout.activity_add_friends_dialog, null, false);
                        dialog.setContentView(view);
                        dialog.setCancelable(true);
                    //dialog.setContentView(R.layout.activity_add_friends_dialog);
                    //dialog.setTitle("Add Friends");
                    add= (ImageView) dialog.findViewById(R.id.add);
                    friends1 = (TextView) dialog.findViewById(R.id.writename);
                    submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
                    add.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {

                            try {
                                friendsCount++;
                                            Log.d("addFriendsActivity", "ashish comes here ");
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                        }
                    });

                    submit_dialog.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {

                            Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
                            startActivity(i);

                        }
                    });
                    dialog.show();   

经过一些谷歌我明白问题可能在r.java或你已经导入android .r但我没有做任何事情在这.. 在此先感谢.... :)

6 个答案:

答案 0 :(得分:2)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/writename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />
    <ImageView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>
<Button
    android:id="@+id/submit_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit" />

答案 1 :(得分:1)

您的java代码中没有错误。它完美你只需要改变你的xml。使用它。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/RelativeLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#FFFFFF"
  tools:context=".AddFriendsCustomActivity" >

 <EditText
    android:id="@+id/writename"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="24dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<ImageView
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/writename"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/writename"
    android:contentDescription="Add Friend"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/submit_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/writename"
    android:layout_below="@+id/add"
    android:layout_marginRight="46dp"
    android:text="Submit" />

</RelativeLayout>

答案 2 :(得分:1)

对于你的xml,我认为它应该是这样的:

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText 
        android:id="@+id/my_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/add_btn" />

    <ImageButton 
        android:id="@+id/add_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/add_btn"
        android:background="@null" />

    <Button 
        android:id="@+id/submit_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/my_edit_text"
        android:text="Submit" />

</RelativeLayout>

请注意可能存在一些拼写错误,因为我没有编译该代码,您应该更改您的实际变量。并按照以下方式创建AlertDialog

AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
View mView = mInflater.inflater(R.layout.my_layout, null, false);
EditText mEdit = (EditText) mView.findViewById(R.id.my_edit_text);
ImageButton mImageBtn = (ImageButton) mView.findViewById(R.id.add_btn);
Button mSubmit = (Button) mView.findViewById(R.id.submit_btn);
mBuilder.setView(mView);
AlertDialog mAlert = mBuilder.create();
mAlert.show();

使用AlertDialog.Builder代替Dialog对您的情况更加轻松(至少在我看来)。您可以将Submit Button添加到AlertDialog作为默认positive按钮。

希望这会对你有所帮助。

答案 3 :(得分:0)

看看你认为的代码

add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);

作为

add= (ImageView) view.findViewById(R.id.add);
friends1 = (EditText) view.findViewById(R.id.writename);

这应该有效。

答案 4 :(得分:0)

试试这个..

        final Dialog dialog = new Dialog(AddFriendsActivity.this);
        dialog.setContentView(R.layout.activity_add_friends_dialog);
        dialog.setCanceledOnTouchOutside(true);

        add= (ImageView) dialog.findViewById(R.id.add);
        EditText friends1 = (EditText) dialog.findViewById(R.id.writename);
        submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
        add.setOnClickListener(new OnClickListener() {
            //@Override
            @SuppressWarnings({ "unchecked", "rawtypes" })
            @SuppressLint("NewApi")
            public void onClick(View v) {
                            friendsCount++;
                            //TextView  textview  = new TextView(context);
                            //textview.setId(friendsCount);
                            //final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                            //params.addRule(RelativeLayout.BELOW, friendsCount--);
                            //textview.setLayoutParams(params);
                            //textview.setVisibility(View.VISIBLE);
                            //relativelayout.addView(textview ,params);
                            Log.d("addFriendsActivity", "ashish comes here ");
            }
        });

        submit_dialog.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
                        startActivity(i);

                    }
                });

        dialog.show();

答案 5 :(得分:0)

您的观点已涵盖在内。 也许你可以尝试将你的父布局设置为Linearlayout,而不是RelativeLayout。