我的布局未填满整个活动宽度

时间:2019-05-02 13:38:46

标签: android layout width fill

我正在使用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;
    }
}

The result when running on the emulator

3 个答案:

答案 0 :(得分:0)

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent" <!--or wrap content -->
    android:layout_height="match_parent"  <!-- or wrap content -->
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

答案 1 :(得分:0)

将回收站视图更改为该视图即可解决此问题。

 <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" />

答案 2 :(得分:0)

我设法解决了。感谢Mike M的提示。

我没有正确调整布局。

我改变了 “视图view = inflater.inflate(R.layout.list_layout,null);
至 “视图= inflater.inflate(R.layout.list_layout,parent,false);”