问题描述
我是Android Studio的新手,所以这可能是一个简单的解决方案(我希望)。
我得到了该项目的详细视图,但是即使将布局宽度和高度设置为“ match_parent”,它也不适合我手机的屏幕。仍然裁剪(线性,相对,约束)的Layout或.xml都没关系,所以我猜它在代码中有所体现。
以下是我认为问题所在的代码:
public class RecyclerViewAdapterBiography extends RecyclerView.Adapter < RecyclerViewAdapterBiography.MyViewHolder > {
private Context myContext;
private List < TimeStamp > myData;
private Dialog myDialog;
public RecyclerViewAdapterBiography(Context myContext,List < TimeStamp > myData) {
this.myContext = myContext;
this.myData = myData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View v;
v = LayoutInflater.from(myContext).inflate(R.layout.biography_item,parent,false);
final MyViewHolder viewHolder = new MyViewHolder(v);
// Detail View of the Biography TimeStamps
// makes the background of the dialog Box transparent
// myDialog.getwindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// OnClickListener for the different Items in the View (to see the detail view)
viewHolder.biography_item_id.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialog = new Dialog(myContext);
myDialog.setContentView(R.layout.biography_detail);
TextView tv_dialog_title = (TextView) myDialog.findViewById(R.id.bio_details_title);
TextView tv_dialog_text = (TextView) myDialog.findViewById(R.id.bio_details_desc);
tv_dialog_title.setText(myData.get(viewHolder.getAdapterPosition()).getTitle());
tv_dialog_text.setText(myData.get(viewHolder.getAdapterPosition()).getDialog());
/*
Toast.makeText(myContext,"Test Click" + String.valueOf
(viewHolder.getAdapterPosition()),Toast.LENGTH_SHORT).show();
*/
myDialog.show();
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder,int position) {
holder.tv_year.setText(myData.get(position).getYear());
holder.tv_title.setText(myData.get(position).getTitle());
}
@Override
public int getItemCount() {
return myData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout biography_item_id;
private TextView tv_year;
private TextView tv_title;
public MyViewHolder(View itemView) {
super(itemView);
biography_item_id = (LinearLayout) itemView.findViewById(R.id.biography_item_id);
tv_year = (TextView) itemView.findViewById(R.id.biography_year);
tv_title = (TextView) itemView.findViewById(R.id.biography_title);
}
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fcfcfc">
<androidx.core.widget.nestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="125dp"
android:layout_height="175dp"
android:background="#2d2d2d"
android:id="@+id/item_biography_img"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:id="@+id/bio_details_title"
android:textStyle="bold"
android:textSize="24sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/bio_details_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:text="Description" />
</LinearLayout>
</androidx.core.widget.nestedScrollView>
我基本上尝试了所有方法,然后发现自己在Internet上发现的东西可以解决这个问题,但是我找不到任何解决方法
解决方法
Dialog
就是这样-它们会弹出您的应用程序内容,仅占据屏幕的一部分,并且在显示它们时您无法与应用程序的其余部分进行交互。
最好看看the docs-就像它说的那样,您并不是真的想直接使用Dialog
类,常见的方法是使用AlertDialog.Builder
并调用设置定义标题,按钮等的方法。方法之一是setView(),这是您可以在其中放置自定义布局的方法,您可以通过先扩大布局来获得它:
view = activity.layoutInflater.inflate(R.layout.biography_detail,null)
对话框具有自己的布局,带有所有圆角和阴影等,因此,当您只进行setContentView
时,就替换了所有样式,并且通常不希望这样做!
如果您不想要该样式,或者想要更多控制(例如使其全屏显示),则应创建一个DialogFragment
-there's a good guide here(我个人认为该网站对于主题概述)
全屏对话框的末尾有一些内容-基本上DialogFragment
的工作方式类似于Fragment
,除了您实现的是onCreateDialog
方法而不是onCreate
之外,在此配置系统为您创建的Dialog
。当您想要显示它时,您可以创建一个实例并使用FragmentManager
来显示它。链接中的第一个示例对此进行了说明,因此我不会在此处粘贴它!