如何居中显示 ImageView?

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

How to center ImageView?

问题

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

ListAdapter.java

  1. import android.content.Context;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.ImageView;
  6. import android.widget.Toast;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import com.example.geometry.R;
  9. import java.util.List;
  10. public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  11. // ... 省略其他代码 ...
  12. class ViewHolder extends RecyclerView.ViewHolder {
  13. public TextView mTv_name;
  14. public ImageView mImg;
  15. public ViewHolder(View itemView) {
  16. super(itemView);
  17. mImg = (ImageView) itemView.findViewById(R.id.img_item);
  18. }
  19. }
  20. }

ItemAdapter.java

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

MainActivity.java

  1. import android.content.Intent;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.widget.ImageButton;
  7. import android.widget.ListView;
  8. import android.widget.RelativeLayout;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.recyclerview.widget.LinearLayoutManager;
  11. import androidx.recyclerview.widget.RecyclerView;
  12. import com.example.geometry.GUI.Builder;
  13. import com.example.geometry.GUI.ItemAdapter;
  14. import com.example.geometry.GUI.ListAdapter;
  15. import com.example.geometry.Output.SolveActivity;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class MainActivity extends AppCompatActivity {
  19. // ... 省略其他代码 ...
  20. private void adapter() {
  21. mAdapter = new ListAdapter(mList, this);
  22. mRecycleView.setAdapter(mAdapter);
  23. mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
  24. mAdapter.notifyDataSetChanged();
  25. }
  26. }

activity_main.xml

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

item_custom.xml

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

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

英文:

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

ListAdapter.java

  1. import android.content.Context;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.ImageView;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import com.example.geometry.R;
  10. import java.util.List;
  11. public class ListAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
  12. private List&lt;ItemAdapter&gt; mList;
  13. private Context mContext;
  14. public ListAdapter(List&lt;ItemAdapter&gt; list, Context context){
  15. super();
  16. mList = list;
  17. mContext = context;
  18. }
  19. @Override
  20. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
  21. View v = LayoutInflater.from(mContext).inflate(R.layout.item_custom, parent, false);
  22. final ViewHolder viewHolder = new ViewHolder(v);
  23. viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. Toast.makeText(v.getContext(), &quot;Input mode = &quot; + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
  27. Builder.mode = viewHolder.getAdapterPosition();
  28. }
  29. });
  30. return viewHolder;
  31. }
  32. @Override
  33. public void onBindViewHolder( RecyclerView.ViewHolder viewHolder, int position) {
  34. ItemAdapter itemAdapter = mList.get(position);
  35. ((ViewHolder) viewHolder).mImg.setImageResource(itemAdapter.getImage());
  36. }
  37. @Override
  38. public int getItemCount() {
  39. return mList.size();
  40. }
  41. class ViewHolder extends RecyclerView.ViewHolder{
  42. public TextView mTv_name;
  43. public ImageView mImg;
  44. public ViewHolder(View itemView) {
  45. super(itemView);
  46. mImg = (ImageView) itemView.findViewById(R.id.img_item);
  47. }
  48. }
  49. }

ItemAdapter.java

  1. public class ItemAdapter {
  2. private int image;
  3. public int getImage() {
  4. return image;
  5. }
  6. public void setImage(int image) {
  7. this.image = image;
  8. }
  9. }

MainActivity.java

  1. import android.content.Intent;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.widget.ImageButton;
  7. import android.widget.ListView;
  8. import android.widget.RelativeLayout;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.recyclerview.widget.LinearLayoutManager;
  11. import androidx.recyclerview.widget.RecyclerView;
  12. import com.example.geometry.GUI.Builder;
  13. import com.example.geometry.GUI.ItemAdapter;
  14. import com.example.geometry.GUI.ListAdapter;
  15. import com.example.geometry.Output.SolveActivity;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class MainActivity extends AppCompatActivity {
  19. private RecyclerView mRecycleView;
  20. private List&lt;ItemAdapter&gt; mList = new ArrayList&lt;&gt;();
  21. private ListAdapter mAdapter;
  22. ListView listView;
  23. RelativeLayout layout;
  24. ImageButton solve_button;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. init();
  29. addList();
  30. adapter();
  31. }
  32. private void init(){
  33. setContentView(R.layout.activity_main);
  34. layout = new RelativeLayout(this);
  35. mRecycleView = findViewById(R.id.recycler_view);//new RecyclerView(this);
  36. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
  37. RelativeLayout.LayoutParams.WRAP_CONTENT);
  38. params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  39. mRecycleView.setLayoutParams(params);
  40. }
  41. private void addList(){
  42. ItemAdapter itemAdapter = new ItemAdapter();
  43. itemAdapter.setImage(R.drawable.circle);
  44. mList.add(itemAdapter);
  45. itemAdapter = new ItemAdapter();
  46. itemAdapter.setImage(R.drawable.line);
  47. mList.add(itemAdapter);
  48. itemAdapter = new ItemAdapter();
  49. itemAdapter.setImage(R.drawable.move);
  50. mList.add(itemAdapter);
  51. itemAdapter = new ItemAdapter();
  52. itemAdapter.setImage(R.drawable.angle);
  53. mList.add(itemAdapter);
  54. itemAdapter = new ItemAdapter();
  55. itemAdapter.setImage(R.drawable.regular_triangle);
  56. mList.add(itemAdapter);
  57. itemAdapter = new ItemAdapter();
  58. itemAdapter.setImage(R.drawable.right_triangle);
  59. mList.add(itemAdapter);
  60. itemAdapter = new ItemAdapter();
  61. itemAdapter.setImage(R.drawable.square);
  62. mList.add(itemAdapter);
  63. itemAdapter = new ItemAdapter();
  64. itemAdapter.setImage(R.drawable.trapeze);
  65. mList.add(itemAdapter);
  66. }
  67. private void adapter(){
  68. mAdapter = new ListAdapter(mList, this);
  69. mRecycleView.setAdapter(mAdapter);
  70. mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
  71. mAdapter.notifyDataSetChanged();
  72. }
  73. }

activity_main.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;match_parent&quot;&gt;
  5. &lt;ImageButton
  6. android:id=&quot;@+id/solve_button&quot;
  7. android:layout_width=&quot;wrap_content&quot;
  8. android:layout_height=&quot;wrap_content&quot;
  9. android:layout_alignParentRight=&quot;true&quot;
  10. android:layout_margin=&quot;30dp&quot;
  11. android:background=&quot;@drawable/ic_calculator&quot; /&gt;
  12. &lt;com.example.geometry.GUI.Builder
  13. android:id=&quot;@+id/builder&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:layout_above=&quot;@+id/recycler_view&quot;
  17. /&gt;
  18. &lt;androidx.recyclerview.widget.RecyclerView
  19. android:id=&quot;@+id/recycler_view&quot;
  20. android:layout_width=&quot;match_parent&quot;
  21. android:layout_height=&quot;wrap_content&quot;
  22. android:layout_alignParentBottom=&quot;true&quot;
  23. /&gt;
  24. &lt;EditText
  25. android:id=&quot;@+id/editText&quot;
  26. android:layout_width=&quot;wrap_content&quot;
  27. android:layout_height=&quot;wrap_content&quot;
  28. android:ems=&quot;10&quot;
  29. android:inputType=&quot;textPersonName&quot;
  30. android:text=&quot;Name&quot; /&gt;
  31. &lt;/RelativeLayout&gt;

item_custom.java

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. android:layout_width=&quot;wrap_content&quot;
  4. android:layout_margin=&quot;2dp&quot;
  5. android:background=&quot;#fff&quot;
  6. android:layout_height=&quot;wrap_content&quot;&gt;
  7. android:layout_gravity=&quot;center&quot;
  8. android:scaleType=&quot;center&quot;&gt;
  9. &lt;ImageView
  10. android:id=&quot;@+id/img_item&quot;
  11. android:layout_width=&quot;wrap_content&quot;
  12. android:contentDescription=&quot;@string/app_name&quot;
  13. android:layout_height=&quot;wrap_content&quot;
  14. android:layout_gravity=&quot;center&quot;
  15. android:scaleType=&quot;center&quot;
  16. android:layout_centerInParent=&quot;true&quot;/&gt;
  17. &lt;/RelativeLayout&gt;

答案1

得分: 1

根据我的代码更改你的item_custom

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_margin="2dp"
  6. android:background="#fff">
  7. <ImageView
  8. android:id="@+id/img_item"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_centerInParent="true"
  12. android:layout_gravity="center"
  13. android:src="@drawable/ic_search"
  14. android:contentDescription="@string/app_name"
  15. android:scaleType="center" />
  16. </RelativeLayout>

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

编码愉快!谢谢!

英文:

Change your item_custom according to my code

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;wrap_content&quot;
  5. android:layout_margin=&quot;2dp&quot;
  6. android:background=&quot;#fff&quot;&gt;
  7. &lt;ImageView
  8. android:id=&quot;@+id/img_item&quot;
  9. android:layout_width=&quot;wrap_content&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:layout_centerInParent=&quot;true&quot;
  12. android:layout_gravity=&quot;center&quot;
  13. android:src=&quot;@drawable/ic_search&quot;
  14. android:contentDescription=&quot;@string/app_name&quot;
  15. android:scaleType=&quot;center&quot; /&gt;
  16. &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:

确定