英文:
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<RecyclerView.ViewHolder> {
private List<ItemAdapter> mList;
private Context mContext;
public ListAdapter(List<ItemAdapter> 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(), "Input mode = " + 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<ItemAdapter> mList = new ArrayList<>();
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
<?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="match_parent">
<ImageButton
android:id="@+id/solve_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="30dp"
android:background="@drawable/ic_calculator" />
<com.example.geometry.GUI.Builder
android:id="@+id/builder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/recycler_view"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</RelativeLayout>
item_custom.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_margin="2dp"
android:background="#fff"
android:layout_height="wrap_content">
android:layout_gravity="center"
android:scaleType="center">
<ImageView
android:id="@+id/img_item"
android:layout_width="wrap_content"
android:contentDescription="@string/app_name"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
答案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
<?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>
Hope it will work definately tested by myself
Happy coding! Thankew!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论