Android 在一个recyclerview中合并两个Drawable,以获得不同的屏幕分辨率
我正在为我的项目设计这张照片 我成功地做到了这一点,但当我使用其他设备(不同的分辨率)时,它看起来像这样糟糕 . 这是我使用的适配器:Android 在一个recyclerview中合并两个Drawable,以获得不同的屏幕分辨率,android,android-drawable,Android,Android Drawable,我正在为我的项目设计这张照片 我成功地做到了这一点,但当我使用其他设备(不同的分辨率)时,它看起来像这样糟糕 . 这是我使用的适配器: public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int RIGHT_BTN = 0; private static final int LEFT_BTN = 1;
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;
}
}
}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
私有静态最终int-RIGHT_BTN=0;
私有静态最终整数左_BTN=1;
ArrayList mList=新的ArrayList();
公共回收服务适配器(ArrayList mList){
this.mList=mList;
}
@凌驾
public int getItemViewType(int位置){
如果((位置%2)==0){
返回权;
}
返回左_BTN;
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup ViewGroup,int viewType){
如果(视图类型==右\u BTN){
View View=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.right\u btn,viewGroup,false);
返回新的ItemViewHolder(视图);
}else if(viewType==LEFT\u BTN){
View View=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.left_btn,viewGroup,false);
返回新的HeaderViewHolder(视图);
}
}
@凌驾
公共无效onBindViewHolder(最终RecyclerView.ViewHolder,最终整型位置){
if(头支架的支架实例){
}else if(ItemViewHolder的持有者实例){
}
}
@凌驾
public int getItemCount(){
返回114;
}
私有类HeaderViewHolder扩展了RecyclerViewView.ViewHolder{
视图;
HeaderViewHolder(视图项视图){
超级(项目视图);
视图=项目视图;
}
}
私有类ItemViewHolder扩展了RecyclerView.ViewHolder{
视图;
ItemViewHolder(视图v){
超级(五);
视图=v;
}
}
}
这是left_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>
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"
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>
我使用drawable绘制圆和线:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_orange_light" />
<size
android:width="150dp"
android:height="3dp" />
</shape>
</item>
<item android:gravity="right">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_orange_light" />
<size
android:width="3dp"
android:height="50dp" />
</shape>
</item>
<item android:gravity="left">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="3dp"
android:color="@android:color/holo_orange_light" />
<solid android:color="#F2000000" />
<size
android:width="100dp"
android:height="100dp" />
</shape>
</item>
</layer-list>
以上代码是我尝试过的,但不起作用。
有什么办法可以解决这个问题吗
喜欢在带有约束的布局中合并到不同的布局吗?
还是实施这一计划的不同想法?
提前谢谢。问题出在您的android:layout\u marginRight=“40dp”的right\u btn.xml和left\u btn.xml中
40dp
在不同的设备上是不一样的,因为设备在dp中可以有不同的屏幕宽度
您需要根据RecyclerView width动态计算偏移量,以使垂直线居中。我建议采用不同的方法。使用单个布局,并根据位置隐藏或显示左右按钮(使用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 );
}