2017-04-25 6 views
1

alertdialogの3つのボタンでカスタムレイアウトを作成しました。幅と高さのレイアウトがコンテンツのラップのみとなるようにレイアウトを作成しようとしています。つまり、余分な幅と高さはありませんが、できません。あなたは私を助けてくれますか?alertdialogラップコンテンツのカスタムレイアウトを作成する方法

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="#00ffffff" 

    > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@drawable/rectangle_menu" 
     > 

    <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/wowButtonId" 
      android:layout_marginLeft="5dp" 
      android:src="@drawable/love_icon" 
      android:background="@drawable/round_button_for_round_menu_like_button" 
     android:layout_gravity="center" 
     /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="35dp" 
     android:id="@+id/blehButtonId" 
     android:layout_marginLeft="5dp" 
     android:src="@drawable/bleh" 
     android:background="@drawable/round_button_for_round_menu_like_button" 
     android:layout_gravity="center" 
     /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/dislikeButtonId" 
     android:layout_marginLeft="5dp" 
     android:src="@drawable/dislike_icon" 
     android:background="@drawable/round_button_for_round_menu_like_button" 
     android:layout_gravity="center" 
     /> 
    </LinearLayout> 
</LinearLayout> 

alertDialogアダプタのコードで実装します:

AlertDialog.Builder builder=new AlertDialog.Builder(context); 
       AlertDialog alertDialog=builder.create(); 
View view1=LayoutInflater.from(context).inflate(R.layout.layout_for_long_like_button_option,null); 

       ImageButton wow=(ImageButton) view1.findViewById(R.id.wowButtonId); 
       ImageButton disLike=(ImageButton) view1.findViewById(R.id.dislikeButtonId); 
       ImageButton bleh=(ImageButton) view1.findViewById(R.id.blehButtonId); 

       wow.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Toast.makeText(context,"wow",Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       disLike.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Toast.makeText(context,"dislike",Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       bleh.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Toast.makeText(context,"bleh",Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       alertDialog.setView(view1); 

       alertDialog.show(); 
+0

DialogFragmentは何を使用していますか? –

+0

no alertdialog recylerviewアダプターのボタンをクリックするオプション – AAA

+0

私にあなたのalertdialogコードを表示 – Arti

答えて

0

...あなたは警告ダイアログを膨らませる、あなたのクラスファイルでこれを試してください。ここ

はalertdialogのための私のカスタムレイアウトであります

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
      Window window = alertDialog.getWindow(); 
      lp.copyFrom(window.getAttributes()); 
//This makes the dialog take up the full width 
      lp.width = WindowManager.LayoutParams.WRAP_CONTENT; 
      lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
      window.setAttributes(lp); 
+0

not working ...... – AAA

0

これを試してみてください。

使用

dialog.show(); 

​​

または

dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
あなたが設定したレイアウトのためのコードの下に使用し、表示することができます
1

ダイアログ

public void showDialog() { 

    LayoutInflater li = LayoutInflater.from(MainActivity.this); 
    View promptsView = li.inflate(R.layout.layout_for_long_like_button_option, null); 

    final TextView txtOk = (TextView) promptsView.findViewById(R.id.txtOk); 
    final TextView txtCancel = (TextView) promptsView.findViewById(R.id.txtCancel); 


    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this); 
    alertDialogBuilder.setView(promptsView); 

    final AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 


    txtOk.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      alertDialog.dismiss(); 

     } 
    }); 


    txtCancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      alertDialog.dismiss(); 
     } 
    }); 


    alertDialog.show(); 
} 
+0

これは動作しています、これは背景を透明にしていますが、私はボタンの長押しのような方法でFacebookにポップアップメニューを表示したいと思いました。ご連絡ありがとうございました。私はあなたのansを正しくマークしました。 – AAA

+0

ダイアログをカスタマイズする方法を確認しましたが、Facebookの絵文字ライブラリのリファレンスがあります。この[https://android-arsenal.com/details/1/3839] – Arti

+0

okをご確認ください。問題ないと助けてくれてありがとう – AAA

0

Summar y:RelativeLayoutタグをカスタムレイアウトのルートに使用

私が書いていたアプリケーションで2つの警告ダイアログが表示されました。一方はコンテンツをラップせず、もう一方はコンテンツをラップしませんでした。 LinearLayoutタグがルートにあるものは、その内容が実際に占有していたスペースよりも、少し高さが増えていました。 LinearLayoutで幅と高さのプロパティをwrap_contentに設定しようとしましたが、役に立たないです。

RelativeLayoutをしっかりとラップしたもの。注意が必要がありますが、内側のLinearLayoutはまだwrap_contentを指定する必要があること、

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    <!-- More widgets --> 
    </LinearLayout> 
</RelativeLayout> 

:これを達成するためのコード構造は次のようになります。もちろん、match_parentと指定することができます。これは、細長いダイアログを表示することを目的としています。その意図は開発者に任せます。

Androidスタジオはまだ警告しますLinearLayoutレイアウトまたはそのRelativeLayoutの親は無駄です。私はこれを問題の解決策とは考えていませんが、単なる修正です。そして、私はいつもこれらのレイアウトの問題をAndroidで扱ってきたことに言及する必要があります。これは、私が思うに、レイアウトに対してコードの変更を抑えるので、より賢明なアプローチです。

関連する問題