简体   繁体   English

Android (Java) - 布局在边缘被切断

[英]Android (Java) - Layout is getting cut off at the edges

I'm pretty new to Android Studio so this might be a simple Solution (i hope).我对 Android Studio 很陌生,所以这可能是一个简单的解决方案(我希望)。

I got this Detail View of an Item but even though the Layout Width and Height are set on "match_parent" it doesn't fit the Screen of my Phone.我得到了这个项目的详细视图,但即使布局宽度和高度设置在“match_parent”上,它也不适合我手机的屏幕。 It does not matter which Layout or.xml I use it is still cropped (linear, relative, constraint), so I guess its something in the code.我使用的 Layout 或 .xml 都没有关系,它仍然被裁剪(线性、相对、约束),所以我猜它在代码中是什么。

裁剪布局的图片

And heres the Code where i think the Problem is:这是我认为问题所在的代码:

    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>

I basically tried everything and rn I'm just a/b with stuff I found on the internet to solve this problem, but I can't find any solution我基本上什么都试过了,我只是用我在互联网上找到的东西来解决这个问题,但我找不到任何解决方案

Dialog s are meant to be like that - they pop up over your app content, only take up part of the screen, and you can't interact with the rest of the app when they're displayed. Dialog的本意就是这样——它们会在你的应用内容上弹出,只占据屏幕的一部分,当它们显示时你不能与应用的 rest 交互。

Best take a look at the docs - like it says you're not really meant to use the Dialog class directly, the common way is to use an AlertDialog.Builder and call the setup methods to define the title, buttons etc. One of the methods is setView() which is how you can put a custom layout in there, which you can get by inflating a layout first:最好看看文档- 就像它说你并不是真的要直接使用Dialog class 一样,常见的方法是使用AlertDialog.Builder并调用设置方法来定义标题、按钮等。其中一个方法是setView() ,这是您可以在其中放置自定义布局的方法,您可以通过先膨胀布局来获得:

view = activity.layoutInflater.inflate(R.layout.biography_detail, null)

Dialogs have their own layout with all the rounded corners and shadowing and such, so when you just do setContentView you're replacing all that styling and you generally don't want that!对话框有自己的布局,所有的圆角和阴影等,所以当你只是做setContentView时,你正在替换所有的样式,你通常不想要那个!


If you don't want that style, or you want more control (like making it full-screen) then you should create a DialogFragment - there's a good guide here (personally I think that site's really useful for overviews of topics)如果你不想要那种风格,或者你想要更多的控制(比如让它全屏),那么你应该创建一个DialogFragment -这里有一个很好的指南(我个人认为该网站对于主题概述非常有用)

There's some stuff on full-screen Dialogs near the end - basically a DialogFragment works like a Fragment , except you implement an onCreateDialog method instead of onCreate , and that's where you configure the Dialog the system has created for you.在接近尾声的全屏 Dialogs 上有一些东西 - 基本上DialogFragment的工作方式与Fragment类似,只是您实现了onCreateDialog方法而不是onCreate ,这就是您配置系统为您创建的Dialog的地方。 When you want to show it, you create an instance and use the FragmentManager to show it.当你想展示它时,你创建一个实例并使用FragmentManager来展示它。 The first example in the link explains it, so I won't paste it here!链接中的第一个示例说明了它,所以我不会在这里粘贴它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM