类微信界面编写
类微信界面编写
总体布局我们将使用 Android 的 `LinearLayout` 布局来实现类微信界面的总体布局。下面是代码:
```xml
android:layout_height="match_parent" android:orientation="vertical"> android:layout_width="match_parent" android:layout_height="wrap_content" android:background="FFFFFF" android:gravity="center_vertical" android:orientation="horizontal"> android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1">
```
主函数下面是主函数的代码:
```javapublic class MainActivity extends AppCompatActivity {
private Button wechatButton;
private Button friendButton;
private Button contactButton;
private Button setButton;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化按钮 wechatButton = findViewById(R.id.wechat_button);
friendButton = findViewById(R.id.friend_button);
contactButton = findViewById(R.id.contact_button);
setButton = findViewById(R.id.set_button);
//设置点击事件 wechatButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
showWechatLayout();
}
});
friendButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
showFriendLayout();
}
});
contactButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
showContactLayout();
}
});
setButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
showSetLayout();
}
});
}
//显示微信界面 private void showWechatLayout() {
wechatButton.setBackgroundResource(R.drawable.wechat_button_selected);
friendButton.setBackgroundResource(R.drawable.friend_button_unselected);
contactButton.setBackgroundResource(R.drawable.contact_button_unselected);
setButton.setBackgroundResource(R.drawable.set_button_unselected);
FrameLayout contentFrameLayout = findViewById(R.id.content_frame_layout);
contentFrameLayout.removeAllViews();
contentFrameLayout.addView(LayoutInflater.from(this).inflate(R.layout.wechat_layout, null));
}
//显示朋友界面 private void showFriendLayout() {
wechatButton.setBackgroundResource(R.drawable.wechat_button_unselected);
friendButton.setBackgroundResource(R.drawable.friend_button_selected);
contactButton.setBackgroundResource(R.drawable.contact_button_unselected);
setButton.setBackgroundResource(R.drawable.set_button_unselected);
FrameLayout contentFrameLayout = findViewById(R.id.content_frame_layout);
contentFrameLayout.removeAllViews();
contentFrameLayout.addView(LayoutInflater.from(this).inflate(R.layout.friend_layout, null));
}
//显示通讯录界面 private void showContactLayout() {
wechatButton.setBackgroundResource(R.drawable.wechat_button_unselected);
friendButton.setBackgroundResource(R.drawable.friend_button_unselected);
contactButton.setBackgroundResource(R.drawable.contact_button_selected);
setButton.setBackgroundResource(R.drawable.set_button_unselected);
FrameLayout contentFrameLayout = findViewById(R.id.content_frame_layout);
contentFrameLayout.removeAllViews();
contentFrameLayout.addView(LayoutInflater.from(this).inflate(R.layout.contact_layout, null));
}
//显示设置界面 private void showSetLayout() {
wechatButton.setBackgroundResource(R.drawable.wechat_button_unselected);
friendButton.setBackgroundResource(R.drawable.friend_button_unselected);
contactButton.setBackgroundResource(R.drawable.contact_button_unselected);
setButton.setBackgroundResource(R.drawable.set_button_selected);
FrameLayout contentFrameLayout = findViewById(R.id.content_frame_layout);
contentFrameLayout.removeAllViews();
contentFrameLayout.addView(LayoutInflater.from(this).inflate(R.layout.set_layout, null));
}
}
```
Gitee你可以在 Gitee 上找到这个项目的源代码: