Android(Java)- 布局在边缘处被裁剪了

huangapple go评论66阅读模式
英文:

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

问题

我对Android Studio还很陌生,所以这可能是一个简单的解决方案(我希望如此)。

我得到了这个项目的详细视图,但即使布局的宽度和高度设置为"match_parent",它仍然不适合我的手机屏幕。无论我使用哪种布局或.xml文件(线性、相对、约束),它仍然被裁剪,所以我猜问题出在代码中。

Android(Java)- 布局在边缘处被裁剪了

这里是我认为问题出在哪里的代码:

public class RecyclerViewAdapterBiography extends RecyclerView.Adapter<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);

        // 生平时间戳的详细视图

        // 使对话框的背景透明
        // myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        // 点击监听器,用于查看详细视图
        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 = myDialog.findViewById(R.id.bio_details_title);
                TextView tv_dialog_text = 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 = itemView.findViewById(R.id.biography_item_id);
            tv_year = itemView.findViewById(R.id.biography_year);
            tv_title = 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>

</RelativeLayout>

我基本上尝试了所有的方法,目前我只是在试验一些在互联网上找到的东西来解决这个问题,但我找不到任何解决方案。

英文:

I'm pretty new to Android Studio so this might be a simple Solution (i hope).

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. 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.

Android(Java)- 布局在边缘处被裁剪了

And heres the Code where i think the Problem is:

    public class RecyclerViewAdapterBiography extends RecyclerView.Adapter &lt; RecyclerViewAdapterBiography.MyViewHolder &gt; {
private Context myContext;
private List &lt; TimeStamp &gt; myData;
private Dialog myDialog;
public RecyclerViewAdapterBiography(Context myContext, List &lt; TimeStamp &gt; 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,&quot;Test Click&quot; + 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);
}
}
}
&lt;RelativeLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#fcfcfc&quot;&gt;
&lt;androidx.core.widget.NestedScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginTop=&quot;10dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:gravity=&quot;center&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;ImageView
android:layout_width=&quot;125dp&quot;
android:layout_height=&quot;175dp&quot;
android:background=&quot;#2d2d2d&quot;
android:id=&quot;@+id/item_biography_img&quot;
android:scaleType=&quot;centerCrop&quot;/&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Title&quot;
android:id=&quot;@+id/bio_details_title&quot;
android:textStyle=&quot;bold&quot;
android:textSize=&quot;24sp&quot;
android:layout_marginTop=&quot;10dp&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/bio_details_desc&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:textSize=&quot;18sp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:text=&quot;Description&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;

</RelativeLayout>

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

答案1

得分: 1

对话框的设计初衷就是如此——它们会在应用内容上方弹出,只占据屏幕的一部分空间,当它们显示时,你无法与应用的其余部分进行交互。

最好查阅一下文档——正如文档所说,你不应该直接使用Dialog类,常见的做法是使用AlertDialog.Builder,并调用设置方法来定义标题、按钮等。其中一个方法是setView(),通过这个方法你可以放置一个自定义布局,在这之前你可以通过膨胀(inflate)一个布局来获取它:

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

对话框有其自己的布局,带有所有的圆角和阴影等效果,因此当你仅使用setContentView时,你会替换掉所有这些样式,通常情况下你是不希望这样做的!


如果你不想使用那种样式,或者你想要更多的控制(比如使其全屏),那么你应该创建一个DialogFragment——这里有一个很好的指南(我个人认为这个网站对于概览式的主题解释非常有用)。

在最后有关于全屏对话框的一些内容——基本上,DialogFragment的工作原理类似于Fragment,只不过你需要实现onCreateDialog方法而不是onCreate,这是你配置系统为你创建的Dialog的地方。当你想要显示它时,你创建一个实例并使用FragmentManager来显示它。链接中的第一个示例对此进行了解释,所以我就不在这里复制了!

英文:

Dialogs 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.

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:

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!


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)

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. When you want to show it, you create an instance and use the FragmentManager to show it. The first example in the link explains it, so I won't paste it here!

huangapple
  • 本文由 发表于 2020年8月16日 06:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63431514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定