自定义对话框中的按钮问题(在 RecyclerView Adapter 中创建)
Problem with a Button inside a Custom Dialog (created in RecyclerView Adapter)
我有一个 RecyclerView
,在每个项目中都有一个(共享)按钮提示包含一个 TextView
和一个 Button
的自定义对话框。
因此,我在 RecyclerView
的 Adapter
中构建了自定义对话框,除了按钮的外观外,一切都很好。
它上面好像有一个白色的覆盖层,当我点击它时,覆盖层稍微消失了。
这是按钮在 xml 中的样子:
这是我得到的:
这是我的自定义对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/AppTheme">
<TextView
android:id="@+id/tv_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:padding="@dimen/fab_margin"
android:textSize="8pt"
/>
<Button
android:id="@+id/btn_copy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_address"
android:theme="@style/button"/>
</LinearLayout>
这是创建对话框的 java 代码:
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.share_dialog, null));
builder.setTitle(view.getContext().getResources().getString(R.string.share));
share = builder.create();
share.show();
注意:我认为问题与充气机有关,尤其是这一行:
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
因为在 MainActivity 中,当我用这个替换它时它工作正常:
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
但我无法使用它,因为我无法从适配器内部获取 Activity。
我希望我的问题很清楚,非常感谢你的帮助。
编辑: @style/button
的代码
<style name="button" parent="ThemeOverlay.AppCompat.Light">
<item name="colorControlHighlight">@color/colorPrimaryDark</item>
<item name="colorButtonNormal">@color/colorGray</item>
<item name="android:textColor">@color/colorWhite</item>
<item name="android:colorAccent">@color/colorPrimary</item>
</style>
将您的 parent="ThemeOverlay.AppCompat.Light" 更改为
ThemeOverlay.AppCompat.Dark.ActionBar
感谢@Tepits 的评论和一些更改..我找到了解决方案。
首先,按照这个建议Call Activity method from adapter
我把LayoutInflater inflater
改成这样:
LayoutInflater inflater= ((MainActivity)context).getLayoutInflater();
然后将 dialog.xml 中的父布局中的 android:layout_width
和 android:layout_height
更改为 wrap_content
并且成功了!
我有一个 RecyclerView
,在每个项目中都有一个(共享)按钮提示包含一个 TextView
和一个 Button
的自定义对话框。
因此,我在 RecyclerView
的 Adapter
中构建了自定义对话框,除了按钮的外观外,一切都很好。
它上面好像有一个白色的覆盖层,当我点击它时,覆盖层稍微消失了。
这是按钮在 xml 中的样子:
这是我得到的:
这是我的自定义对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/AppTheme">
<TextView
android:id="@+id/tv_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:padding="@dimen/fab_margin"
android:textSize="8pt"
/>
<Button
android:id="@+id/btn_copy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_address"
android:theme="@style/button"/>
</LinearLayout>
这是创建对话框的 java 代码:
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.share_dialog, null));
builder.setTitle(view.getContext().getResources().getString(R.string.share));
share = builder.create();
share.show();
注意:我认为问题与充气机有关,尤其是这一行:
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
因为在 MainActivity 中,当我用这个替换它时它工作正常:
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
但我无法使用它,因为我无法从适配器内部获取 Activity。
我希望我的问题很清楚,非常感谢你的帮助。
编辑: @style/button
<style name="button" parent="ThemeOverlay.AppCompat.Light">
<item name="colorControlHighlight">@color/colorPrimaryDark</item>
<item name="colorButtonNormal">@color/colorGray</item>
<item name="android:textColor">@color/colorWhite</item>
<item name="android:colorAccent">@color/colorPrimary</item>
</style>
将您的 parent="ThemeOverlay.AppCompat.Light" 更改为 ThemeOverlay.AppCompat.Dark.ActionBar
感谢@Tepits 的评论和一些更改..我找到了解决方案。
首先,按照这个建议Call Activity method from adapter
我把LayoutInflater inflater
改成这样:
LayoutInflater inflater= ((MainActivity)context).getLayoutInflater();
然后将 dialog.xml 中的父布局中的 android:layout_width
和 android:layout_height
更改为 wrap_content
并且成功了!