我的工具栏未显示在CardView中

时间:2018-08-18 18:40:10

标签: android android-recyclerview kotlin cardview

我想制作一个CardView回收器适配器以显示2个东西(如第一个图像)。我有2个xml布局,一个带有工具栏,另一个带有CardView。问题是CardView正在显示在开始时屏幕上没有显示工具栏(仅显示箭头,但仍然...)。

The way i want it

这就是我得到的:

The way i have it

这是我的第一个xml

<?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:layout_width="match_parent"
    android:id="@+id/constr"
    android:layout_height="match_parent"
    tools:context=".SettingsAct.SettingsActiv">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarSettings"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorAccent"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/todayToolbarSettingsBtn"
        android:layout_width="39dp"
        android:layout_height="35dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/ic_arrow_back_black_24dp"
        app:layout_constraintEnd_toEndOf="@+id/toolbarSettings"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/toolbarSettings" />

    <TextView
        android:id="@+id/settingsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="230dp"
        android:layout_marginTop="8dp"
        android:text="@string/settingsText"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/toolbarSettings" />


</android.support.constraint.ConstraintLayout>

这是我的第二个xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="71dp"
            android:layout_marginTop="8dp"
            android:gravity="left"
            android:paddingBottom="4dp"
            android:paddingLeft="16dp"
            android:text="TextView"
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="42dp"
            android:gravity="bottom"
            android:text="TextView"
            android:textSize="18sp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height=".5dp"
            android:background="@color/cardview_dark_background" />

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

这是我的主要活动

class SettingsActiv : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)


        val constraintLayout = findViewById(R.id.constr) as ConstraintLayout
        val recyclerView =  RecyclerView(this)
        val series = ArrayList<SeriesModel>()
        series.add(SeriesModel("Unit of length","Meter"))
        series.add(SeriesModel("Unit of temperature","Celcius"))
        val adapter = MyAdapter(series)
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        recyclerView.adapter = adapter
        constraintLayout.addView(recyclerView)

    }
}

2 个答案:

答案 0 :(得分:1)

setSupportActionBar(findViewById(R.id.toolbarSettings))
supportActionBar?.setDisplayHomeAsUpEnabled(true)

答案 1 :(得分:1)

用LinearLayout替换约束布局,将其方向设置为垂直,并使用此代码添加recyclerview:

val linearLayout = findViewById(R.id.constr) as LinearLayout
recyclerView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
linearLayout.addView(recyclerView, 1)