我迫切需要帮助。数天来,我一直在努力解决自己的问题,而我对继续进行我的项目失去了任何渴望。我似乎似乎听不懂,所以请您能帮助我。
我正在尝试创建一个activity_all_friends.xml,其中包含朋友列表。我的想法是,我将拥有一个包含自定义视图的线性布局(“ friendView”)。
我已经创建了friendView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/friendImage"
android:layout_width="100dp"
android:layout_height="match_parent"
android:contentDescription="Person Image"
app:srcCompat="@drawable/person2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/friendName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Name of friend"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionInPersonIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/in_person" />
<TextView
android:id="@+id/interactionInPersonText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionVideoIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:tint="#292828"
app:srcCompat="@android:drawable/presence_video_online" />
<TextView
android:id="@+id/interactionVideoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionTextIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/text_icon" />
<TextView
android:id="@+id/interactionTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionPhoneIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:tint="#353535"
app:srcCompat="@drawable/phone_icon" />
<TextView
android:id="@+id/interactionPhoneText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
无论如何,当我尝试在activity_all_friends布局上绘制此新视图时,它显示为空白:
我也想通过单击右上角的“创建新”按钮来添加这些新朋友。我已将setOnClickListener分配给该按钮。按下按钮有效,新元素似乎过时了,我只是看不到它们。
我的FriendView类:
public class FriendView extends LinearLayout {
private Context context;
private String friend;
public FriendView(Context context) {
super(context);
init(null,0);
}
public FriendView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs,0);
}
public FriendView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs,defStyle);
init(attrs,0);
}
private void init(AttributeSet attrs, int defStyle){
}
public String getFriend() {
return friend;
}
public void setFriend(String friend) {
this.friend = friend;
}
public View getView(View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.friend_view, null);
TextView friendName = (TextView) view.findViewById(R.id.friendView);
TextView interactionInPersonText = (TextView) view.findViewById(R.id.interactionInPersonText);
TextView interactionVideoText = (TextView) view.findViewById(R.id.interactionVideoText);
TextView interactionTextText = (TextView) view.findViewById(R.id.interactionTextText);
TextView interactionPhoneText = (TextView) view.findViewById(R.id.interactionPhoneText);
// TODO
friendName.setText("Temp random");
interactionInPersonText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionVideoText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionTextText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionPhoneText.setText(Long.toString(Math.round(Math.random() * 300)));
return view;
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.BLUE);
}
}
任何人都知道为什么会这样吗?我的理解是,我需要编写要生成的xml元素(在我的情况下为friend_view.xml),我要编写动态处理该元素发生情况的类(在我的情况下为friendView类),并且那么我想将其链接到要生成它的布局(在我的情况下为activity_all_friends.xml)。这是正确的吗?
2个其他小问题:我在布局文件夹中有我的friends_view.xml,对吗?属性从何而来(attr.xml)?我想点击我的API并动态更改数字和朋友的名字。
感谢您的所有帮助!
您应该使用inflater.inflate(R.layout.friend_view,this,true)将布局扩展到自定义视图中,而不分配任何变量,并且应该在init函数中调用它。