【问题标题】:Android (Java) - Layout is getting cut off at the edgesAndroid (Java) - 布局在边缘被切断
【发布时间】:2020-12-05 10:45:29
【问题描述】:

我对 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>

我基本上什么都试过了,我只是用我在互联网上找到的东西来解决这个问题,但我找不到任何解决方案

【问题讨论】:

    标签: java android mobile layout crop


    【解决方案1】:

    Dialogs 就是这样设计的 - 它们会在您的应用内容上弹出,只占据屏幕的一部分,当它们显示时您无法与应用的其余部分进行交互。

    最好看看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 来展示它。链接中的第一个例子说明了,这里就不贴了!

    【讨论】:

      猜你喜欢
      • 2016-11-03
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2021-12-30
      相关资源
      最近更新 更多