在一个recyclerview中合并两个drawables以用于不同的屏幕res

时间:2017-08-29 10:36:44

标签: android android-drawable

我正在尝试为我的项目设计这张照片

photo

我成功完成了它,但是当我使用其他设备(不同分辨率)时,它看起来很糟糕

image

这是我使用的适配器:

for (key, val) in Document(doc["groups"]) ?? [] {
    print("Value is \(val)")
}

这是left_btn.xml

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int RIGHT_BTN = 0;
    private static final int LEFT_BTN = 1;
    ArrayList<String> mList = new ArrayList<>();

    public RecyclerViewAdapter(ArrayList<String> mList) {

        this.mList = mList;
    }


    @Override
    public int getItemViewType(int position) {
        if ((position % 2) == 0) {
            return RIGHT_BTN;
        }
        return LEFT_BTN;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        if (viewType == RIGHT_BTN) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.right_btn, viewGroup, false);
            return new ItemViewHolder(view);

        } else if (viewType == LEFT_BTN){
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.left_btn, viewGroup, false);
            return new HeaderViewHolder(view);

        }


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


        if (holder instanceof HeaderViewHolder) {

        } else if (holder instanceof ItemViewHolder) {


        }


    }


    @Override
    public int getItemCount() {
        return 114;
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        View View;

        HeaderViewHolder(View itemView) {
            super(itemView);
            View = itemView;
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        View View;

        ItemViewHolder(View v) {
            super(v);
            View = v;
        }
    }
}

right_btn.xml

<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="wrap_content"
    android:orientation="horizontal">


</android.support.constraint.ConstraintLayout>

我使用drawable绘制圆圈和线条:

<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="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintRight_toLeftOf="@+id/button2"
    tools:layout_editor_absoluteY="73dp">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:layout_marginTop="0dp"
        android:background="@drawable/right_circle_with_line"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

以上代码是我尝试但不工作的代码。 有什么想法可以解决这个问题 比如在约束的布局上合并到不同的布局? 或实现这一点的不同想法? 提前谢谢你。

2 个答案:

答案 0 :(得分:1)

我建议采用不同的方法。使用单个布局并根据位置隐藏或显示左右按钮(使用View.VISIBLE和View.INVISIBLE),您可以将线条直接放在布局的中心。像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">

<ImageButton
    android:src="@drawable/left_button"
    android:id="@+id/left"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

<FrameLayout
    android:layout_width="4dp"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_light"/>

<ImageButton
    android:src="@drawable/right_button"
    android:id="@+id/right"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

</LinearLayout>

然后在代码中

if ( position % 2 == 0 ) { 
    findViewById( R.id.left ).setVisibile( View.VISIBLE ); 
    findViewById( R.id.right ).setVisibile( View.INVISIBLE ); 
} else { 
    findViewById( R.id.left ).setVisibile( View.INVISIBLE ); 
    findViewById( R.id.right ).setVisibile( View.VISIBLE ); 
}

答案 1 :(得分:0)

问题出在你的android:layout_marginRight="40dp"中的right_btn.xml和left_btn.xml中。 40dp在不同的设备上不一样,因为设备在dp中的屏幕宽度可能不同。

您需要根据RecyclerView宽度动态计算offsed,以使垂直线居中。