新手提醒
我正在使用ConstraintLayout填充我的ListView。我认为行高会根据内容自动调整,但这似乎并没有发生。应该吗?
My Fragment代码:
public class ConversationListFragment extends ListFragment {
public interface OnItemSelectedListener {
public void onItemSelected(Conversation conversation);
}
OnItemSelectedListener selectionListener;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
Messaging controller = MessagingFactory.getInstance().getController();
Conversation[] conversations = controller.amsConversations.getActiveConversations();
setListAdapter(new ConversationListAdapter(getActivity(), conversations));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
selectionListener = (OnItemSelectedListener) activity;
} catch (ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement OnItemSelectedListener");
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
Conversation conversation = (Conversation) getListAdapter().getItem(position);
selectionListener.onItemSelected(conversation);
}
}
我的适配器getView方法:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.lpmessaging_ui_conversation_list_entry, parent, false);
convertView.setMinimumHeight(105); // not helping
viewHolder = new ViewHolder();
viewHolder.remoteName = (TextView) convertView.findViewById(R.id.conversation_list_remote_name);
viewHolder.lastMessage = (TextView) convertView.findViewById(R.id.conversation_list_last_message);
viewHolder.unreadCount = (TextView) convertView.findViewById(R.id.conversation_list_unread_counter);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// set textView text here
return convertView;
}
我的布局XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/conversation_list_remote_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Brand Name"
android:textAlignment="viewStart"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
app:layout_constraintEnd_toStartOf="@+id/verticalGuideline"
app:layout_constraintStart_toStartOf="parent"
tools:layout_conversion_absoluteHeight="24dp"
tools:layout_conversion_absoluteWidth="272dp" />
<TextView
android:id="@+id/conversation_list_last_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Last Message"
android:textAlignment="viewStart"
app:layout_constraintEnd_toStartOf="@+id/verticalGuideline"
app:layout_constraintStart_toStartOf="@+id/conversation_list_remote_name"
app:layout_constraintTop_toTopOf="@+id/horizontalGuideline" />
<TextView
android:id="@+id/conversation_list_unread_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@drawable/lpinfra_ui_scroll_down_unread_counter_shape"
android:text="1"
android:textColor="@color/scroll_down_indicator_unread_counter_text_color"
android:textSize="@dimen/regular_text_size"
app:layout_constraintTop_toTopOf="@+id/horizontalGuideline"
app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/verticalGuideline"
/>
<android.support.constraint.Guideline
android:id="@+id/horizontalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<android.support.constraint.Guideline
android:id="@+id/verticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="45dp" />
</android.support.constraint.ConstraintLayout>
输出:
我试图在膨胀的视图上设置最小高度,但是在SP或像素或点中是什么?我尝试了35和105的值,但它没有改变任何东西。
如何正确调整ListView行的大小?
答案 0 :(得分:0)
与iOS约束系统一样,您需要从容器视图顶部到底部的约束。 ConstraintLayout中的所有对象都没有对父级的顶部或底部的约束。
从顶部TextView向父级添加一个约束,从底部TextView向父级添加一个约束,这将让android正确调整行的大小。