【问题标题】:how to automatically align long text in next line in TextView under CardView如何在 CardView 下的 TextView 中自动对齐下一行​​的长文本
【发布时间】:2016-09-06 19:44:42
【问题描述】:

我在 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);

    }
}

}

谢谢。

【问题讨论】:

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


    【解决方案1】:

    使用这个:

    yourTextView.append("\n anotherTextPart")
    

    【讨论】:

    • 此文本将被动态添加。我在这里设置只是为了查看结果
    【解决方案2】:

    首先 - 如果您的字符串没有空格,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>
    

    主要活动:

    <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>
    

    结果:

    【讨论】:

    • 我试过这个。但它只添加空格而不是在新行中对齐字符串
    • 我已经尝试过您的代码,它适用于一个卡片视图。可能在充气过程中出现问题。你如何在布局上为你的卡片充气?
    • 只使用 RecycleView。能否请您分享您的代码。
    • 不,先生,当我使用 android:text="vasckhvacnv.acv.ahjscv.asjhcvajhvjahxvjahcvascjhvacjhvcasj"this 在我的 xml 中它工作正常但问题是当我从 java 类设置文本时。这仍然不起作用。
    • 我已经修改了代码 - 无论如何它都在工作。您使用的是什么版本的 cardview 支持库?还有哪个目标,编译版本?
    【解决方案3】:

    通过使用解决

    表格布局

    并使用特定的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多