Android仿微信个人页面代码
时间: 2023-11-03 08:53:27 浏览: 81
以下是一个简单的仿微信个人页面的 Android 代码示例:
1. 在布局文件中添加控件:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/avatar"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/avatar"
android:scaleType="centerCrop"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="昵称"
android:textSize="18sp"
android:textColor="#000000"
android:layout_alignTop="@+id/avatar"
android:layout_toRightOf="@+id/avatar"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/wechatid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信号:wechatid"
android:textSize="14sp"
android:textColor="#666666"
android:layout_below="@+id/nickname"
android:layout_toRightOf="@+id/avatar"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地区:北京"
android:textSize="14sp"
android:textColor="#666666"
android:layout_below="@+id/wechatid"
android:layout_toRightOf="@+id/avatar"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"/>
<Button
android:id="@+id/button_add_friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加朋友"
android:textColor="#ffffff"
android:background="@drawable/button_add_friend"
android:layout_below="@+id/avatar"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/moments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朋友圈"
android:textSize="16sp"
android:textColor="#000000"
android:layout_below="@+id/button_add_friend"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"/>
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:layout_below="@+id/moments"
android:layout_marginTop="10dp"/>
</RelativeLayout>
```
2. 在代码中设置头像、昵称等信息:
```java
public class MainActivity extends AppCompatActivity {
private ImageView mAvatar;
private TextView mNickname;
private TextView mWechatId;
private TextView mLocation;
private Button mAddFriend;
private TextView mMoments;
private GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAvatar = (ImageView) findViewById(R.id.avatar);
mNickname = (TextView) findViewById(R.id.nickname);
mWechatId = (TextView) findViewById(R.id.wechatid);
mLocation = (TextView) findViewById(R.id.location);
mAddFriend = (Button) findViewById(R.id.button_add_friend);
mMoments = (TextView) findViewById(R.id.moments);
mGridView = (GridView) findViewById(R.id.gridview);
// 设置头像
mAvatar.setImageResource(R.drawable.avatar);
// 设置昵称
mNickname.setText("昵称");
// 设置微信号
mWechatId.setText("微信号:wechatid");
// 设置地区
mLocation.setText("地区:北京");
// 设置添加朋友按钮
mAddFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 添加朋友逻辑
}
});
// 设置朋友圈
mMoments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 进入朋友圈界面
}
});
// 设置 GridView
mGridView.setAdapter(new ImageAdapter(this));
}
private class ImageAdapter extends BaseAdapter {
private Context mContext;
private ImageAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 10);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// 图片资源数组
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6,
R.drawable.image7, R.drawable.image8,
R.drawable.image9
};
}
}
```
这样,就可以实现一个简单的仿微信个人页面了。当然,你可以根据自己的需求进行更改和扩展。
阅读全文