我有CardView。在那裏我有一個相對佈局,裏面有4個文本視圖。 在我的活動中,我有一個線性佈局,在一個Recycler視圖。 我有一個GridlayoutManager設置爲回收視圖。它是水平滾動的。Cardview不適合屏幕
的卡越來越在卡的文本視圖文本的大小創建。
我的代碼:
Activity.java
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_page);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
data_list = new ArrayList<>();
load_data();
gridLayoutManager = new GridLayoutManager(this, 1,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new CustomAdapter(this,data_list);
recyclerView.setAdapter(adapter);
AdapterClass:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context context;
private List<MyData> my_data;
public CustomAdapter(Context context, List<MyData> my_data) {
this.context = context;
this.my_data = my_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
itemView.setMinimumWidth(parent.getMeasuredWidth());
itemView.setMinimumHeight(parent.getMeasuredHeightAndState());
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
holder.userstory.setText(my_data.get(position).getUserstory());
holder.tasks.setText(my_data.get(position).getTask());
holder.actual.setText(my_data.get(position).getActuals());
holder.estimate.setText(my_data.get(position).getTodos());
holder.seekBar.setMax(my_data.get(position).getEstimate());
holder.seekBar.setProgress(my_data.get(position).getActual());
}
@Override
public int getItemCount()
{
return my_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView userstory,tasks,actual,estimate;
public SeekBar seekBar;
public ViewHolder(View itemView)
{
super(itemView);
userstory=(TextView) itemView.findViewById(R.id.tvUserStory);
tasks=(TextView) itemView.findViewById(R.id.tvTask);
actual=(TextView) itemView.findViewById(R.id.tvActual);
estimate=(TextView) itemView.findViewById(R.id.tvEstimate);
seekBar=(SeekBar)itemView.findViewById(R.id.seekBar);
}
}
}
Activity.xml
<?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="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
Card.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linear1"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="1dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tvUserStory"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:text="@string/us" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tvTask"
android:layout_below="@+id/tvUserStory"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:text="@string/task" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tvActual"
android:layout_below="@+id/tvTask"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:text="@string/actual" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tvEstimate"
android:layout_alignParentStart="true"
android:layout_below="@+id/tvActual"
android:layout_alignParentEnd="true"
android:text="@string/estimate" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/circularSeekBar1"
android:layout_below="@+id/tvEstimate"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:indeterminate="false"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我只是想要卡的大小適合screen.ie如果文本不適合一行應該到下一行,而不是水平擴大。
如果您跨度爲1,則使用線性佈局管理器 –
將左右邊距也添加到textview中。 –
是的..我的跨度是1 ..我想要一張卡片一次和相同的卡片大小 – Akshay