微信界面安卓代码
时间: 2023-07-03 13:17:09 浏览: 69
以下是一个简单的微信界面的安卓代码示例,包括一个主Activity和三个Fragment:聊天列表、通讯录和发现。
MainActivity.java:
```
public class MainActivity extends AppCompatActivity {
private FrameLayout frameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = findViewById(R.id.frame_layout);
// 默认显示聊天列表Fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, new ChatListFragment())
.commit();
}
// 切换到聊天列表Fragment
public void switchToChatList() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, new ChatListFragment())
.addToBackStack(null)
.commit();
}
// 切换到通讯录Fragment
public void switchToContacts() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, new ContactsFragment())
.addToBackStack(null)
.commit();
}
// 切换到发现Fragment
public void switchToDiscover() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, new DiscoverFragment())
.addToBackStack(null)
.commit();
}
}
```
activity_main.xml:
```
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
ChatListFragment.java:
```
public class ChatListFragment extends Fragment {
public ChatListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chat_list, container, false);
// TODO:显示聊天列表
return view;
}
}
```
fragment_chat_list.xml:
```
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO:聊天列表UI -->
</LinearLayout>
```
ContactsFragment.java:
```
public class ContactsFragment extends Fragment {
public ContactsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_contacts, container, false);
// TODO:显示通讯录列表
return view;
}
}
```
fragment_contacts.xml:
```
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO:通讯录列表UI -->
</LinearLayout>
```
DiscoverFragment.java:
```
public class DiscoverFragment extends Fragment {
public DiscoverFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_discover, container, false);
// TODO:显示发现页面内容
return view;
}
}
```
fragment_discover.xml:
```
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO:发现页面UI -->
</LinearLayout>
```
以上代码仅为示例,具体实现需要根据实际需求进行调整。
阅读全文