【问题标题】:My layout does not fill the whole activity width [duplicate]我的布局没有填满整个活动宽度[重复]
【发布时间】:2019-09-21 01:16:14
【问题描述】:

我正在使用 AndroidStudio3.2.1。我正在设计一个回收器视图,当使用模拟器运行它时,它不会填满活动的整个宽度。为什么会这样?

我尝试了“fill_parent”、“layout_weight”,但都没有奏效。我有一个 CardView,其中包含应出现在视图持有者中的组件。

主要活动的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

每个ViewHolder中会显示的list_items的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="0"
        app:cardBackgroundColor="@color/colorPrimary"
        app:cardCornerRadius="5dp"
        app:cardElevation="6dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textViewTeacher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Section 1"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Database 1"
                    android:textSize="18sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical">

                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>
        </LinearLayout>


    </android.support.v7.widget.CardView>
</LinearLayout>

MainActivity.java 的代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SectionAdapter adapter;
    private List<Section> sectionList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
        sectionList = new ArrayList<Section>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        Section sec = null;
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);

        adapter = new SectionAdapter(this,sectionList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);

    }
}

适配器的代码

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class SectionAdapter extends RecyclerView.Adapter<SectionAdapter.SectionViewHolder> {
    private Context context;
    private List<Section> sectionList;

    public SectionAdapter(Context context, List<Section> sectionList) {
        this.context = context;
        this.sectionList = sectionList;
    }

    @NonNull
    @Override
    public SectionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view  = inflater.inflate(R.layout.list_layout,null);
        return new SectionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SectionViewHolder holder, int position) {
        Section sec = sectionList.get(position);
        holder.setValues(sec);
    }

    @Override
    public int getItemCount() {
        return sectionList.size();
    }

    class SectionViewHolder extends RecyclerView.ViewHolder
    {
        private TextView nameTV;
        private TextView idTV;
        private TextView teacherTV;

        public SectionViewHolder(View itemView) {
            super(itemView);
            nameTV = itemView.findViewById(R.id.textViewName);
            idTV = itemView.findViewById(R.id.textViewID);
            teacherTV = itemView.findViewById(R.id.textViewTeacher);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = getAdapterPosition();
                    Toast.makeText(context, "Laptop "+pos+" has been clicked", Toast.LENGTH_LONG).show();
                }
            });
        }

        public void setValues(Section sec)
        {
            nameTV.setText(sec.getName());
            idTV.setText(sec.getId());
            teacherTV.setText(sec.getTeacher());
        }
    }
}

Section.java 的代码

public class Section {
    private String name;
    private String id;
    private String teacher;

    public Section(String name, String id, String teacher) {
        this.name = name;
        this.id = id;
        this.teacher = teacher;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
}

【问题讨论】:

  • 您确定您在RecyclerView.Adapter 中正确扩展了项目布局吗?当每个项目的数据不同时,项目的宽度是否不同?
  • 我将提供我的 java 代码。可能有人发现出了什么问题。
  • 是的,将onCreateViewHolder() 中的inflate() 调用更改为inflater.inflate(R.layout.list_layout, parent, false)

标签: android layout width fill


【解决方案1】:

我设法修复它。感谢 Mike M 的提示。

我没有正确地扩展布局。

我变了 "查看视图 = inflater.inflate(R.layout.list_layout,null);
到 "视图视图 = inflater.inflate(R.layout.list_layout,parent,false);"

【讨论】:

    【解决方案2】:

    将您的回收站视图更改为此应该可以解决它。

     <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    【讨论】:

    • 很遗憾,它没有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多