我正在努力解决这个问题:我想创建一个“whatsapp like”对话列表,我想将图像,用户名和最后一条消息捆绑在一个外部布局中,我可以通过编程方式调用然后再添加一个新的,但由于某种原因,这不起作用。
chat_link_layout.xml
<TextView
android:text="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
android:layout_toRightOf="@+id/profileimage"
android:layout_toEndOf="@+id/profileimage"
android:textAppearance="@android:style/TextAppearance.Material.Medium"
android:textColor="@android:color/black"
android:textStyle="normal|bold"
android:layout_marginTop="5px"/>
<TextView
android:text="Last Message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lastmessage"
android:layout_below="@+id/username"
android:layout_toRightOf="@+id/profileimage"
android:layout_toEndOf="@+id/profileimage"
android:textAppearance="@android:style/TextAppearance.Material.Medium"
android:textColor="@android:color/black" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/profileimage"
android:layout_marginRight="10px" />
MainActivity.java的onCreate函数的一部分,我尝试调用XML布局
View chatLinkLayout = LayoutInflater.from(getApplication()).inflate(R.layout.chat_link_layout, null);
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.content_main);
constraintLayout.setBackgroundColor(Color.BLACK);
constraintLayout.addView(chatLinkLayout);
constraintLayout.addView(new CalendarView(getApplicationContext()));
日历显示没有问题。
ChatLinkLayout.java
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class ChatLinkLayout extends FrameLayout {
private TextView username;
private TextView lastmessage;
private ImageView profileimage;
public ChatLinkLayout(Context context) {
super(context);
username = (TextView) findViewById(R.id.username);
lastmessage = (TextView) findViewById(R.id.lastmessage);
profileimage = (ImageView) findViewById(R.id.profileimage);
}
}
你们有没有任何建议?
谢谢。