如何在CardView下的TextView中自动对齐下一行​​中的长文本

时间:2016-09-06 19:44:42

标签: java android textview android-recyclerview android-cardview

我在 android.support.v7.widget.CardView 下使用TextView。但是,每当我尝试设置长文本时,TextView都没有给出完整的结果。

 List<User> userList = new ArrayList<>();
    userList.add(new User(R.mipmap.ic_launcher,
            "Amit", "9988776655",
            "amit@sonevalley.comamit@sonevalley.comamit@sonevalley.comamit@sonevalley.comamit@sonevalley.comamit@sonevalley.com"));

这是我的java代码(作为示例),这在应用程序中显示如下: for this first one 'Amit'

如何解决这个问题?如果文本很长,那么它会自动将其设置为下一行。

这是我的完整Cardview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<android.support.v7.widget.CardView
    android:id="@+id/cvSingleUser"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp">

        <ImageView
            android:id="@+id/ivProfilePic"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginRight="16dp" />

        <TextView
            android:id="@+id/tvProfileName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_toRightOf="@id/ivProfilePic" />

        <TextView
            android:id="@+id/tvPhoneNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tvProfileName"
            android:layout_toRightOf="@id/ivProfilePic" />

        <TextView
            android:id="@+id/tvEmailId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:scrollHorizontally="true"
            android:layout_below="@id/tvPhoneNumber"
            android:layout_toRightOf="@id/ivProfilePic" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

这是java代码的快照:Java Snap Shot

这是java完整代码

public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.UserViewHolder>{

private List<MainActivity.User> userList;
private Context context;
public AllUsersAdapter(List<MainActivity.User> userList, Context context) {
    this.userList = userList;
    this.context = context;
}

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.single_cardview_layout, null);
    UserViewHolder userViewHolder = new UserViewHolder(view);
    return userViewHolder;
}

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
    MainActivity.User user = userList.get(position);

    String a=user.getEmailId().toString();
    holder.tvProfileName.setText(user.getProfileName());
    holder.tvPhoneNumber.setText(user.getPhoneNumber());
    holder.tvEmailId.setText(a);
}

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

public static class UserViewHolder extends RecyclerView.ViewHolder {


    TextView tvProfileName;
    TextView tvPhoneNumber;
    TextView tvEmailId;
    public UserViewHolder(View itemView) {
        super(itemView);
        tvProfileName = (TextView) itemView.findViewById(R.id.tvProfileName);
        tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
        tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);

    }
}

}

谢谢。

3 个答案:

答案 0 :(得分:0)

使用它:

yourTextView.append("\n anotherTextPart")

答案 1 :(得分:0)

首先 - 如果你的字符串没有空格,TextView将无法在多行中正确地格式化文本。

尝试设置以下属性:

<TextView
 ...
 android:maxLines="4"/>

更新这是我的测试,即使没有上面的标志也能正常运行:

MainActivity.java:

private class ViewHolder extends RecyclerView.ViewHolder {
    public TextView email;
    public ViewHolder(View itemView) {
        super(itemView);
        email = (TextView) itemView.findViewById(R.id.tvEmailId);
    }
}

private class Adapter extends RecyclerView.Adapter<ViewHolder> {
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_card, parent, false);
        return new ViewHolder(card);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.email.setText("test@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.com");
    }

    @Override
    public int getItemCount() {
        return 5;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    .....
    RecyclerView view = (RecyclerView) findViewById(R.id.recyclerView);

    view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    view.setAdapter(new Adapter());
}

user_card.xml(将layout_height更改为wrap_content):

<?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:padding="16dp">

    <android.support.v7.widget.CardView
        android:id="@+id/cvSingleUser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp">

            <ImageView
                android:id="@+id/ivProfilePic"
                android:layout_width="60dp"
                android:src="@drawable/user"
                android:layout_height="60dp"
                android:layout_marginRight="16dp" />

            <TextView
                android:id="@+id/tvProfileName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="test test test"
                android:layout_toRightOf="@id/ivProfilePic" />

            <TextView
                android:id="@+id/tvPhoneNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test"
                android:layout_below="@id/tvProfileName"
                android:layout_toRightOf="@id/ivProfilePic" />

            <TextView
                android:id="@+id/tvEmailId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="false"
                android:text="test@gmail.comtest@gmail.comtest@gmail.comvtest@gmail.com"
                android:scrollHorizontally="true"
                android:layout_below="@id/tvPhoneNumber"
                android:layout_toRightOf="@id/ivProfilePic" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>
</RelativeLayout>

main_activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

结果:

enter image description here

答案 2 :(得分:0)

使用

解决
  

TableLayout

并使用特定的width.full代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:padding="16dp">

<android.support.v7.widget.CardView
    android:id="@+id/cvSingleUser"
    android:layout_marginRight="2dp"
    android:layout_marginLeft="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="270dip"
        android:layout_height="wrap_content">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:layout_gravity="center_horizontal">
        <TextView
            android:id="@+id/tvProfileName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="test test test"/>
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:layout_gravity="center_horizontal">
        <TextView
            android:id="@+id/tvPhoneNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:text="test"
            android:layout_below="@id/tvProfileName" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        <TextView
            android:id="@+id/tvEmailId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="test@gmail.comtest@gmail.comtest@gmail.comvtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comvtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comvtest@gmail.comtest@gmail.comtest@gmail.comtest@gmail.comvtest@gmail.com"
            android:layout_below="@id/tvPhoneNumber" />
        </TableRow>
    </TableLayout>

</android.support.v7.widget.CardView>