0

我正在使用TextView下的android.support.v7.widget.CardView。但是,每當我嘗試設置長文本時,TextView都沒有給出完整的結果。如何在CardView下的TextView中的下一行自動對齊長文本

List<User> userList = new ArrayList<>(); 
    userList.add(new User(R.mipmap.ic_launcher, 
      "Amit", "9988776655", 
      "[email protected]@[email protected]@[email protected]@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); 

    } 
} 

}

謝謝。

回答

0

使用本:

yourTextView.append("\n anotherTextPart") 
+0

這個文本將被動態地添加。我在這裏設置只是爲了看到結果 –

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("[email protected]@[email protected]@[email protected]@[email protected]@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="[email protected]@[email protected]@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

+0

我試過這個,但它只添加空白不對齊字符串在新行 –

+0

我試過你的代碼,它爲一個cardview工作。可能在充氣過程中出現問題你如何在佈局上充氣你的卡? – j2ko

+0

只需使用RecycleView.can即可,請分享您的代碼。 –

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="[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@gmail.com" 
      android:layout_below="@id/tvPhoneNumber" /> 
     </TableRow> 
    </TableLayout> 

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

相關問題