MainActivity Finished!

master
Toyga 3 years ago
parent 5378a5877a
commit 2823c8da2a

@ -1,13 +1,17 @@
package com.example.leudaemialikeme; package com.example.leudaemialikeme;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@ -28,11 +32,118 @@ public class MainActivity extends AppCompatActivity {
private TextView textCommunity; private TextView textCommunity;
private TextView textMessage; private TextView textMessage;
private TextView textMy; private TextView textMy;
//底部导航点击事件监听器
private View.OnClickListener onClickListener = new View.OnClickListener() {
public void onClick(View v) {
//先将四个 ImageButton 置为灰色
resetImgs();
//根据点击的 Tab 切换不同的页面及设置对应的 ImageButton 为绿色
switch (v.getId()) {
case R.id.tab_home:
selectTab(0);
break;
case R.id.tab_community:
selectTab(1);
break;
case R.id.tab_message:
selectTab(2);
break;
case R.id.tab_my:
selectTab(3);
break;
}
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
initFragments(); //初始化数据
initViews(); //初始化控件
initEvents(); //初始化事件
initFirstRun(0);//第一次运行初始化界面,第一个碎片
}
private void initFirstRun(int i) {
resetImgs(); //重置所有 Tab
selectTab(i); //显示第 i 个碎片
}
private void selectTab(int i) {
//根据点击的 Tab 设置对应的 ImageButton 为绿色
switch (i) {
case 0:
imgHome.setImageResource(R.mipmap.img_home_pressed);
textHome.setTextColor(getResources().getColor(R.color.hair_grey));
break;
case 1:
imgCommunity.setImageResource(R.mipmap.img_community_pressed);
textCommunity.setTextColor(getResources().getColor(R.color.hair_grey));
break;
case 2:
imgMessage.setImageResource(R.mipmap.img_message_pressed);
textMessage.setTextColor(getResources().getColor(R.color.hair_grey));
break;
case 3:
imgMy.setImageResource(R.mipmap.img_my_pressed);
textMy.setTextColor(getResources().getColor(R.color.hair_grey));
break;
}
//设置当前点击的 Tab 所对应的页面
setCurrentFragment(i);
}
private void setCurrentFragment(int i) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction trans = fm.beginTransaction();
trans.replace(R.id.frag_layout, fragmentList.get(i));
trans.commit();
//Log.e("fragment", "ok");
}
private void resetImgs() {
imgHome.setImageResource(R.mipmap.img_home_normal);
imgCommunity.setImageResource(R.mipmap.img_community_normal);
imgMessage.setImageResource(R.mipmap.img_message_normal);
imgMy.setImageResource(R.mipmap.img_my_normal);
textHome.setTextColor(getResources().getColor(R.color.light_grey));
textCommunity.setTextColor(getResources().getColor(R.color.light_grey));
textMessage.setTextColor(getResources().getColor(R.color.light_grey));
textMy.setTextColor(getResources().getColor(R.color.light_grey));
}
private void initEvents() {
tabHome.setOnClickListener(onClickListener);
tabCommunity.setOnClickListener(onClickListener);
tabMessage.setOnClickListener(onClickListener);
tabMy.setOnClickListener(onClickListener);
}
private void initViews() {
tabHome = (LinearLayout)findViewById(R.id.tab_home);
tabCommunity = (LinearLayout)findViewById(R.id.tab_community);
tabMessage = (LinearLayout)findViewById(R.id.tab_message);
tabMy = (LinearLayout)findViewById(R.id.tab_my);
imgHome = (ImageButton)findViewById(R.id.tab_home_img);
imgCommunity = (ImageButton)findViewById(R.id.tab_community_img);
imgMessage = (ImageButton)findViewById(R.id.tab_message_img);
imgMy = (ImageButton)findViewById(R.id.tab_my_img);
textHome = (TextView)findViewById(R.id.tab_home_text);
textCommunity = (TextView)findViewById(R.id.tab_community_text);
textMessage = (TextView)findViewById(R.id.tab_message_text);
textMy = (TextView)findViewById(R.id.tab_my_text);
}
private void initFragments() {
fragmentList = new ArrayList<Fragment>();
fragmentList.add(new IndexFragment());
fragmentList.add(new CommunityFragment());
fragmentList.add(new MessageFragment());
fragmentList.add(new MyFragment());
} }
} }

@ -17,10 +17,11 @@
android:clickable="false" android:clickable="false"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/tab_home_pressed" android:src="@mipmap/img_home_pressed"
android:background="@color/white"
/> />
<TextView <TextView
android:id="@+id/text_home" android:id="@+id/tab_home_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="首页"/> android:text="首页"/>
@ -37,7 +38,8 @@
android:clickable="false" android:clickable="false"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/tab_video_normal" /> android:background="@color/white"
android:src="@mipmap/img_community_normal" />
<TextView <TextView
android:id="@+id/tab_community_text" android:id="@+id/tab_community_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -56,9 +58,10 @@
android:clickable="false" android:clickable="false"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/tab_contact_normal" /> android:background="@color/white"
android:src="@mipmap/img_message_normal" />
<TextView <TextView
android:id="@+id/text_contact" android:id="@+id/tab_message_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="消息"/> android:text="消息"/>
@ -75,7 +78,8 @@
android:clickable="false" android:clickable="false"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/tab_my_normal" /> android:background="@color/white"
android:src="@mipmap/img_my_normal" />
<TextView <TextView
android:id="@+id/tab_my_text" android:id="@+id/tab_my_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 653 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 728 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 B

@ -3,4 +3,7 @@
<color name="colorPrimary">#008577</color> <color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color> <color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color> <color name="colorAccent">#D81B60</color>
<color name="light_grey">#cdcdcd</color>
<color name="hair_grey">#66c18c</color>
<color name="white">#fff</color>
</resources> </resources>
Loading…
Cancel
Save