为什么 RecyclerView 覆盖了工具栏(ToolBar)?

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

Why is the RecyclerView inflating over the ToolBar?

问题

I'm having an issue with my app when I inflate a recyclerview. Inside the recyclerview, some cards are inflated, and when they come onto the screen, the toolbar disappears, and the settings button can't be used.

Using the latest SDK and library versions.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">
    </androidx.appcompat.widget.Toolbar>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/conteudoRSS"
        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_toBottomOf="@+id/toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>

linha.xml -> Here I get the cardview which I use to show the items on the recyclerview

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/CardView.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView
            android:id="@+id/titulo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Title"
            android:textColor="#000000"
            tools:text="Titulo Noticia"
            app:layout_constraintEnd_toStartOf="@+id/imagem"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/dataPublicacao"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="#8A000000"
            tools:text="18 Setembro 2020"
            app:layout_constraintEnd_toStartOf="@+id/imagem"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/titulo" />

        <ImageView
            android:id="@+id/imagem"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="12dp"
            tools:background="@color/colorAccent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/url"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="invisible"
            tools:ignore="MissingConstraints"
            app:layout_constraintTop_toBottomOf="@+id/dataPublicacao" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

MainActivity.java

package br.ufpe.cin.android.rss;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.prof.rssparser.Article;
import com.prof.rssparser.Channel;
import com.prof.rssparser.OnTaskCompleted;
import com.prof.rssparser.Parser;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String urlFeed;
    private Toolbar mTopToolbar;
    RecyclerView conteudoRSS;
    List<Article> noticias;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        conteudoRSS = findViewById(R.id.conteudoRSS);
        mTopToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mTopToolbar);
        SharedPreferences preferences = getSharedPreferences
                ("user_preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("feed1", getString(R.string.feed1));
        editor.putString("feed2", getString(R.string.feed2));
        editor.putString("feed3", getString(R.string.feed3));
        editor.apply();

        if (preferences.contains("rssfeed")) {
            urlFeed = preferences.getString("rssfeed", getString(R.string.feed_padrao));
        } else {
            editor.putString("rssfeed", getString(R.string.feed_padrao));
            editor.apply();
            urlFeed = preferences.getString("rssfeed", getString(R.string.feed_padrao));
        }

        conteudoRSS = new RecyclerView(this);
        conteudoRSS.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_settings) {
            startActivity(new Intent(
                    this, PreferenciasActivity.class));
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Parser p = new Parser.Builder().build();
        p.onFinish(
                new OnTaskCompleted() {
                    @Override
                    public void onTaskCompleted(Channel channel) {
                        noticias = channel.getArticles();
                        runOnUiThread(
                                () -> {
                                    RssAdapter adapter = new RssAdapter(
                                            getApplicationContext(),
                                            noticias
                                    );
                                    conteudoRSS.setAdapter(adapter);
                                    setContentView(conteudoRSS);
                                }
                        );
                    }

                    @Override
                    public void onError(Exception e) {
                        Log.e("RSS_APP", e.getMessage());
                    }
                }
        );
        p.execute(urlFeed);
    }

    protected void onResume() {
        super.onResume();
    }

    private String getRssFeed(String feed) throws IOException {
        InputStream in

<details>
<summary>英文:</summary>

I&#39;m having an issue with my app when I inflate an recyclerview. 
Inside the recyclerview, some cards are inflated and when it comes out to the screen, the toolbar disappears and the settings button can&#39;t be used.

Using the latest SDK and libraries versions.

**activity_main.xml:**

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:minHeight=&quot;?attr/actionBarSize&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
tools:ignore=&quot;MissingConstraints&quot;&gt;
&lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/conteudoRSS&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/toolbar&quot; /&gt;

</androidx.constraintlayout.widget.ConstraintLayout>

**linha.xml** -&gt; here I get the cardview which I use to show the items on the recyclerview

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;5dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/titulo&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;16dp&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Title&quot;
android:textColor=&quot;#000000&quot;
tools:text=&quot;Titulo Noticia&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/imagem&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/dataPublicacao&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;10dp&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Body1&quot;
android:textColor=&quot;#8A000000&quot;
tools:text=&quot;18 Setembro 2020&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/imagem&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/titulo&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/imagem&quot;
android:layout_width=&quot;140dp&quot;
android:layout_height=&quot;140dp&quot;
android:layout_margin=&quot;12dp&quot;
tools:background=&quot;@color/colorAccent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/url&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
android:visibility=&quot;invisible&quot;
tools:ignore=&quot;MissingConstraints&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/dataPublicacao&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

</androidx.cardview.widget.CardView>

**MainActivity.java**

package br.ufpe.cin.android.rss;

import androidx.appcompat.app.AppCompatActivity;
package br.ufpe.cin.android.rss;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.prof.rssparser.Article;
import com.prof.rssparser.Channel;
import com.prof.rssparser.OnTaskCompleted;
import com.prof.rssparser.Parser;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private String urlFeed;
private Toolbar mTopToolbar;
RecyclerView conteudoRSS;
List<Article> noticias;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conteudoRSS = findViewById(R.id.conteudoRSS);
mTopToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mTopToolbar);
SharedPreferences preferences = getSharedPreferences
(&quot;user_preferences&quot;, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(&quot;feed1&quot;, getString(R.string.feed1));
editor.putString(&quot;feed2&quot;, getString(R.string.feed2));
editor.putString(&quot;feed3&quot;, getString(R.string.feed3));
editor.apply();
if(preferences.contains(&quot;rssfeed&quot;)){
urlFeed = preferences.getString(&quot;rssfeed&quot;, getString(R.string.feed_padrao));
} else {
editor.putString(&quot;rssfeed&quot;, getString(R.string.feed_padrao));
editor.apply();
urlFeed = preferences.getString(&quot;rssfeed&quot;, getString(R.string.feed_padrao));
}
conteudoRSS = new RecyclerView(this);
conteudoRSS.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
startActivity(new Intent(
this, PreferenciasActivity.class));
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onStart() {
super.onStart();
Parser p = new Parser.Builder().build();
p.onFinish(
new OnTaskCompleted() {
@Override
public void onTaskCompleted(Channel channel) {
noticias = channel.getArticles();
runOnUiThread(
() -&gt; {
RssAdapter adapter = new RssAdapter(
getApplicationContext(),
noticias
);
conteudoRSS.setAdapter(adapter);
setContentView(conteudoRSS);
}
);
}
@Override
public void onError(Exception e) {
Log.e(&quot;RSS_APP&quot;,e.getMessage());
}
}
);
p.execute(urlFeed);
}
protected void onResume() {
super.onResume();
}
private String getRssFeed(String feed) throws IOException {
InputStream in = null;
String rssFeed;
try {
URL url = new URL(feed);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
byte[] response = out.toByteArray();
rssFeed = new String(response, StandardCharsets.UTF_8);
} finally {
if (in != null) {
in.close();
}
}
return rssFeed;
}

}
conteudoRSS = new RecyclerView(this);
conteudoRSS.setLayoutManager(new LinearLayoutManager(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
startActivity(new Intent(
this, PreferenciasActivity.class));
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onStart() {
super.onStart();
Parser p = new Parser.Builder().build();
p.onFinish(
new OnTaskCompleted() {
@Override
public void onTaskCompleted(Channel channel) {
noticias = channel.getArticles();
runOnUiThread(
() -&gt; {
RssAdapter adapter = new RssAdapter(
getApplicationContext(),
noticias
);
conteudoRSS.setAdapter(adapter);
setContentView(conteudoRSS);
}
);
}
@Override
public void onError(Exception e) {
Log.e(&quot;RSS_APP&quot;,e.getMessage());
}
}
);
p.execute(urlFeed);
}
protected void onResume() {
super.onResume();
}
private String getRssFeed(String feed) throws IOException {
InputStream in = null;
String rssFeed;
try {
URL url = new URL(feed);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
byte[] response = out.toByteArray();
rssFeed = new String(response, StandardCharsets.UTF_8);
} finally {
if (in != null) {
in.close();
}
}
return rssFeed;
}

}


**ItemRssViewHolder.java**

package br.ufpe.cin.android.rss;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class ItemRssViewHolder extends RecyclerView.ViewHolder {
TextView titulo = null;
ImageView image = null;
TextView data = null;
TextView url = null;

public ItemRssViewHolder(View itemCard) {
super(itemCard);
this.titulo = itemCard.findViewById(R.id.titulo);
this.image = itemCard.findViewById(R.id.imagem);
this.data = itemCard.findViewById(R.id.dataPublicacao);
this.url = itemCard.findViewById(R.id.url);
itemCard.setOnClickListener(
v -&gt; {
String uri = url.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
itemCard.getContext().startActivity(intent);
}
);
}

}


**RssAdapter.java**

package br.ufpe.cin.android.rss;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.prof.rssparser.Article;
import com.squareup.picasso.Picasso;

import java.util.List;

import static br.ufpe.cin.android.rss.R.layout.linha;

public class RssAdapter extends RecyclerView.Adapter <ItemRssViewHolder> {

List&lt;Article&gt; noticias;
Context context;
public RssAdapter(Context c, List&lt;Article&gt; noticias) {
this.noticias = noticias;
this.context = c;
}
public int getCount() {
return noticias.size();
}
public Object getItem(int i) {
return noticias.get(i);
}
@NonNull
@Override
public ItemRssViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.linha, parent, false);
ItemRssViewHolder viewHolder = new ItemRssViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ItemRssViewHolder viewHolder, int i) {
Article noticia = noticias.get(i);
viewHolder.titulo.setText(noticia.getTitle());
viewHolder.data.setText(&quot;Publicado em: &quot; + noticia.getPubDate().substring(0, 22));
viewHolder.url.setText(noticia.getLink());
String url = noticias.get(i).getImage();
Picasso.with(context)
.load(url)
.into(viewHolder.image);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemCount() {
return noticias.size();
}

}


</details>
# 答案1
**得分**: 2
因为你将 Recyclerview 的顶部与父级 ConstraintLayout 的顶部对齐,所以它会覆盖工具栏。
将 `app:layout_constraintTop_toTopOf="parent"` 替换为 `app:layout_constraintTop_toBottomOf="@id/toolbar"`,以将 Recyclerview 的顶部与工具栏的底部对齐。
在你的工具栏中添加以下属性:
- `app:layout_constraintBottom_toTopOf="@id/conteudoRSS"`
- `app:layout_constraintTop_toTopOf="parent"`
- `app:layout_constraintStart_toStartOf="parent"`
- `app:layout_constraintEnd_toEndOf="parent"`
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/conteudoRSS"
tools:ignore="MissingConstraints">
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/conteudoRSS"
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_toBottomOf="@id/toolbar"/> <!-- 将 app:layout_constraintTop_toTopOf="parent" 替换为这里的约束 -->
</androidx.constraintlayout.widget.ConstraintLayout>

在这种情况下,LinearLayout 也是一个不错的选择,它比 ConstraintLayout 更简单。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">
    </androidx.appcompat.widget.Toolbar>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/conteudoRSS"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
英文:

Because you align the top of Recyclerview to the top of parent (ConstraintLayout) so it overs the toolbar.

Replace app:layout_constraintTop_toTopOf=&quot;parent&quot; with app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot; to align the top of Recyclerview to the bottom of Toolbar

Add these attrs to your Toolbar:

  • app:layout_constraintBottom_toTopOf=&quot;@id/conteudoRSS&quot;
  • app:layout_constraintTop_toTopOf=&quot;parent&quot;
  • app:layout_constraintStart_toStartOf=&quot;parent&quot;
  • app:layout_constraintEnd_toEndOf=&quot;parent&quot;
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 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:id=&quot;@+id/linearLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:minHeight=&quot;?attr/actionBarSize&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintBottom_toTopOf=&quot;@id/conteudoRSS&quot;
tools:ignore=&quot;MissingConstraints&quot;&gt;
&lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/conteudoRSS&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot;/&gt; &lt;!-- replace app:layout_constraintTop_toTopOf=&quot;parent&quot;  --&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

LinearLayout is also good, it's simpler than ConstraintLayout in this case

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:id=&quot;@+id/linearLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?attr/actionBarSize&quot;&gt;
&lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/conteudoRSS&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;/&gt;
&lt;/LinearLayout&gt;

答案2

得分: 0

尝试一下这个

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 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:id=&quot;@+id/linearLayout&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;androidx.appcompat.widget.Toolbar
        android:id=&quot;@+id/toolbar&quot;
        android:minHeight=&quot;?attr/actionBarSize&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/conteudoRSS&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;@+id/toolbar&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
    &lt;/androidx.appcompat.widget.Toolbar&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/conteudoRSS&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;0dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot; /&gt;&lt;!--TODO:Edit--&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

如果这不起作用,尝试将 android:layout_marginTop=&quot;?attr/actionBarSize&quot; 添加到 RecyclerView 中。

希望能有所帮助。如果需要进一步解释,请随时提问...

英文:

Try this

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 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:id=&quot;@+id/linearLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:minHeight=&quot;?attr/actionBarSize&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintBottom_toTopOf=&quot;@id/conteudoRSS&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/toolbar&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
&lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/conteudoRSS&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot; /&gt;&lt;!--TODO:Edit--&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

If this does not work, try adding android:layout_marginTop=&quot;?attr/actionBarSize&quot; to RecyclerView.

Hope this helps. Feel free to ask for clarifications...

答案3

得分: 0

问题在Vishnu的帮助下得到了解决,他回答了这个问题。
问题似乎是我在主活动上创建了一个新的RecyclerView,而不是使用布局xml文件中定义的RecyclerView。
要解决这个问题,只需要从主活动中删除两行代码:

conteudoRSS = new RecyclerView(this);
setContentView(conteudoRSS);

并且只保留RecyclerView的第一次定义,如下所示:

RecyclerView conteudoRSS;
conteudoRSS = findViewById(R.id.conteudoRSS);
英文:

Issue fixed with the help of Vishnu, who's answered this question.
It seems the problem was that I was creating a new recycleview on the main activity instead of using the recycleview defined by the layout xml file.
To fix that, it was only necessary to remove two lines from the main activity:

conteudoRSS = new RecyclerView(this);
setContentView(conteudoRSS);

And maintain only the first definition of the recycleview, as follows:

RecyclerView conteudoRSS;
conteudoRSS = findViewById(R.id.conteudoRSS);

huangapple
  • 本文由 发表于 2020年9月28日 09:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64094990.html
匿名

发表评论

匿名网友

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

确定