约束布局显示不正确

时间:2019-04-28 11:29:16

标签: java android-recyclerview android-constraintlayout

我正在开发新闻Android应用,我一直关注流行的新闻应用ui,但下面的图片显示得非常模糊 current screenshot of app enter image description here

我要实现的

以下screenshot I want to achieve屏幕截图 enter image description here

在我的article_list.xml下面

  

  

<ImageView
    android:id="@+id/articleImage"
    android:layout_width="408dp"
    android:layout_height="208dp"
    android:layout_gravity="center"
    app:layout_constraintRight_toRightOf="parent"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/articleAuthor"
    android:layout_width="136dp"
    android:layout_height="75dp"
    android:gravity="start"
    android:text="@string/article_author"
    android:textSize="12sp"
    app:layout_constraintLeft_toLeftOf="parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteY="219dp" />

<TextView
    android:id="@+id/articleTitle"
    android:layout_width="160dp"
    android:layout_height="78dp"
    android:text="@string/article_title"
    android:textColor="@color/colorBlack"
    android:textSize="28sp"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="3dp"
    tools:layout_editor_absoluteY="325dp" />


<TextView
    android:id="@+id/articleTime"
    android:layout_width="160dp"
    android:layout_height="78dp"
    android:text="Article Time"
    android:textSize="28sp"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="3dp"
    tools:layout_editor_absoluteY="459dp" />

<ImageButton
    android:id="@+id/articleShare"
    android:layout_width="139dp"
    android:layout_height="60dp"
    android:src="@drawable/ic_share"
    android:background="@color/colorWhite"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="115dp"
    tools:layout_editor_absoluteY="558dp" />


<ImageButton
    android:id="@+id/articleFavorite"
    android:layout_width="139dp"
    android:layout_height="60dp"
    android:background="@color/colorWhite"
    android:src="@drawable/ic_bookmark"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="272dp"
    tools:layout_editor_absoluteY="558dp" />
     

     

在我的适配器类下面

公共类ArticleAdapter扩展了RecyclerView.Adapter {

public static final String urlKey = "urlKey";
public static final String imageKey = "imageKey";
public ArticleAdapter.ClickListener listener;
private ClipboardManager myClipboard;
private ClipData myClip;
Context context;
private List<Article> articles = new ArrayList<>();


public ArticleAdapter(List<Article> articles, SportNews sportNews) {
    this.articles = articles;
    this.listener = listener;

}

public ArticleAdapter(ArticleAdapter.ClickListener clickListener) {
}

@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.article_list, null);
    return new CustomViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int position) {
    Article article = articles.get(position);
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");

    Date d = new Date();
    try {
        d = input.parse(article.getPublishedAt());
    } catch (ParseException e) {
        e.printStackTrace();
    }


    String formatted = output.format(d);
    if (article != null) {
        customViewHolder.articleAuthor.setText(article.getAuthor());
        customViewHolder.articleTitle.setText(article.getTitle());
        customViewHolder.articleTime.setText(formatted);
        Picasso.get().load(article.getUrlToImage()).into(customViewHolder.articleImage);
    }
    customViewHolder.itemView.setOnClickListener(v -> {
        Intent intent = new Intent(v.getContext(), DetailActivity.class);

        intent.putExtra("urlKey", article.getUrl());

        v.getContext().startActivity(intent);
    });
    customViewHolder.articleShare.setOnClickListener(v -> {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String articleDescription = article.getDescription();
        String articleTitle = article.getTitle();
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, articleDescription);
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, articleTitle);
        v.getContext().startActivity((Intent.createChooser(sharingIntent, "Share using")));


    });

    customViewHolder.articleFavorite.setOnClickListener(v -> {
        myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


        myClip = ClipData.newPlainText("label", customViewHolder.articleTitle.getText().toString());
        myClipboard.setPrimaryClip(myClip);
        Toast.makeText(v.getContext(), "Copied to clipboard", Toast.LENGTH_SHORT).show();

    });

}


@Override
public int getItemCount() {
    if (articles == null) return 0;
    return articles.size();
}

public interface ClickListener {
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.articleAuthor)
    TextView articleAuthor;
    @BindView(R.id.articleTitle)
    TextView articleTitle;
    @BindView(R.id.articleTime)
    TextView articleTime;
    @BindView(R.id.articleShare)
    ImageButton articleShare;
    @BindView(R.id.articleFavorite)
    ImageButton articleFavorite;
    @BindView(R.id.articleImage)
    ImageView articleImage;


    public CustomViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
      }
  }
}

2 个答案:

答案 0 :(得分:0)

您的问题是您没有在视图上设置正确的约束。

  1. tools属性仅影响预览,而不影响实际实现。因此,每次您遇到类似tools:layout_editor_absoluteX="3dp"的内容时,您只是在预览中移动视图,而不是实际的实现。
  2. 您需要在视图上设置正确的约束。例如,如果您的图像需要位于父视图app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"的右上方
  3. 您需要彼此约束视图,例如,如果articleTitle需要位于父级的左侧和articleAuthor的下方,则需要在articleTitle内进行设置:{{ 1}}和app:layout_constraintLeft_toLeftOf="parent"

我强烈建议您删除所有app:layout_constraintTop_toBottomOf="@id/articleAuthor",IDE会实际上告诉您每个视图缺少哪些约束。

最后,您应该看一下此页底部的示例:https://developer.android.com/training/constraint-layout

答案 1 :(得分:0)

您缺少许多约束,此外,您在视图上使用了固定大小。 由于使用固定尺寸的其他手机具有不同的屏幕尺寸,因此您的屏幕无法响应所有屏幕尺寸。

如果要避免这种情况,可以使用Guildelines来确定视图的宽度和高度,这样布局就可以适合所有屏幕尺寸。


以下是使用ConstraintLayout的示例:

<?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<ImageView
    android:id="@+id/articleImage"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline6"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline4"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/articleAuthor"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:gravity="start"
    android:text="article_author"
    android:textSize="12sp"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/articleTitle"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="article_title"
    android:textSize="28sp"
    app:layout_constraintBottom_toBottomOf="@+id/articleImage"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/articleAuthor" />


<TextView
    android:id="@+id/articleTime"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:text="Article Time"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/guideline5"
    app:layout_constraintEnd_toStartOf="@+id/articleFavorite"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/articleTitle"
    app:layout_constraintTop_toTopOf="@+id/guideline6" />

<ImageButton
    android:id="@+id/articleShare"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/articleTime"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/articleFavorite"
    app:layout_constraintTop_toTopOf="@+id/articleTime" />


<ImageButton
    android:id="@+id/articleFavorite"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/articleTime"
    app:layout_constraintEnd_toStartOf="@+id/articleShare"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/articleTime"
    app:layout_constraintTop_toTopOf="@+id/articleTime" />

<android.support.constraint.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<android.support.constraint.Guideline
    android:id="@+id/guideline6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.3" />

<android.support.constraint.Guideline
    android:id="@+id/guideline5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.4"/>
</android.support.constraint.ConstraintLayout>

它看起来像这样:

enter image description here

如果您要更改视图尺寸,只需移动辅助线,所有布局都将相应调整。


我可以给您一些提示-不要使用tools:ignore="MissingConstraints",因为此属性只会影响预览,并且您在预览和运行时看到的内容将不相同。