如何居中显示 ImageView?

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

How to center ImageView?

问题

我有一个RecyclerAdapter,其中有几张图片水平放置。如何使每个item_custom中的ImageView居中。

ListAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.geometry.R;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // ... 省略其他代码 ...

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTv_name;
        public ImageView mImg;
        
        public ViewHolder(View itemView) {
            super(itemView);
            mImg = (ImageView) itemView.findViewById(R.id.img_item);
        }
    }
}

ItemAdapter.java

public class ItemAdapter {
    // ... 省略其他代码 ...
}

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.geometry.GUI.Builder;
import com.example.geometry.GUI.ItemAdapter;
import com.example.geometry.GUI.ListAdapter;
import com.example.geometry.Output.SolveActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // ... 省略其他代码 ...
    
    private void adapter() {
        mAdapter = new ListAdapter(mList, this);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mAdapter.notifyDataSetChanged();
    }
}

activity_main.xml

<!-- ... 省略其他代码 ... -->

item_custom.xml

<!-- ... 省略其他代码 ... -->

请注意,我已经按照你的要求将代码部分进行了翻译,如果有任何问题,请随时告知。

英文:

I have a RecyclerAdapter in which several images are horizontally placed. How to center each ImageView in item_custom

ListAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.geometry.R;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {

    private List&lt;ItemAdapter&gt; mList;
    private Context mContext;
    public ListAdapter(List&lt;ItemAdapter&gt; list, Context context){
        super();
        mList = list;
        mContext = context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_custom, parent, false);

        final ViewHolder viewHolder = new ViewHolder(v);

        viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), &quot;Input mode = &quot; + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
                Builder.mode = viewHolder.getAdapterPosition();
            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder viewHolder, int position) {
        ItemAdapter itemAdapter = mList.get(position);
        ((ViewHolder) viewHolder).mImg.setImageResource(itemAdapter.getImage());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView mTv_name;
        public ImageView mImg;
        public ViewHolder(View itemView) {

            super(itemView);
            mImg = (ImageView) itemView.findViewById(R.id.img_item);

        }
    }
}

ItemAdapter.java

public class ItemAdapter {
    private int image;

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }
}

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.geometry.GUI.Builder;
import com.example.geometry.GUI.ItemAdapter;
import com.example.geometry.GUI.ListAdapter;
import com.example.geometry.Output.SolveActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecycleView;
    private List&lt;ItemAdapter&gt; mList = new ArrayList&lt;&gt;();
    private ListAdapter mAdapter;
    ListView listView;
    RelativeLayout layout;

    ImageButton solve_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        addList();
        adapter();
    }

    private void init(){
        setContentView(R.layout.activity_main);
        layout = new RelativeLayout(this);
        mRecycleView = findViewById(R.id.recycler_view);//new RecyclerView(this);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mRecycleView.setLayoutParams(params);
    }
    private void addList(){
        ItemAdapter itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.circle);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.line);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.move);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.angle);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.regular_triangle);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.right_triangle);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.square);
        mList.add(itemAdapter);

        itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.trapeze);
        mList.add(itemAdapter);
    }

    private void adapter(){
        mAdapter = new ListAdapter(mList, this);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mAdapter.notifyDataSetChanged();

    }


}

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;ImageButton
        android:id=&quot;@+id/solve_button&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentRight=&quot;true&quot;
        android:layout_margin=&quot;30dp&quot;
        android:background=&quot;@drawable/ic_calculator&quot; /&gt;

    &lt;com.example.geometry.GUI.Builder
        android:id=&quot;@+id/builder&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_above=&quot;@+id/recycler_view&quot;
        /&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recycler_view&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentBottom=&quot;true&quot;
    /&gt;

    &lt;EditText
        android:id=&quot;@+id/editText&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:ems=&quot;10&quot;
        android:inputType=&quot;textPersonName&quot;
        android:text=&quot;Name&quot; /&gt;

&lt;/RelativeLayout&gt;

item_custom.java

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_margin=&quot;2dp&quot;
    android:background=&quot;#fff&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;
    android:layout_gravity=&quot;center&quot;
    android:scaleType=&quot;center&quot;&gt;
    &lt;ImageView
        android:id=&quot;@+id/img_item&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:contentDescription=&quot;@string/app_name&quot;
        android:layout_height=&quot;wrap_content&quot; 
        android:layout_gravity=&quot;center&quot;
        android:scaleType=&quot;center&quot;
        android:layout_centerInParent=&quot;true&quot;/&gt;


&lt;/RelativeLayout&gt;

答案1

得分: 1

根据我的代码更改你的item_custom

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#fff">

    <ImageView
        android:id="@+id/img_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@drawable/ic_search"
        android:contentDescription="@string/app_name"
        android:scaleType="center" />

</RelativeLayout>

希望它能正常工作,我已经亲自测试过了

编码愉快!谢谢!

英文:

Change your item_custom according to my code

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_margin=&quot;2dp&quot;
    android:background=&quot;#fff&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/img_item&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:layout_gravity=&quot;center&quot;
        android:src=&quot;@drawable/ic_search&quot;
        android:contentDescription=&quot;@string/app_name&quot;
        android:scaleType=&quot;center&quot; /&gt;


&lt;/RelativeLayout&gt;

Hope it will work definately tested by myself

Happy coding! Thankew!

huangapple
  • 本文由 发表于 2020年5月5日 12:33:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61605833.html
匿名

发表评论

匿名网友

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

确定