Android之自定义简单小框架

MainActivity.java

 1 package com.example.awenapp;
 2 
 3 
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.app.Fragment;
 7 import android.app.FragmentManager;
 8 import android.app.FragmentTransaction;
 9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.LinearLayout;
14 import android.widget.RadioButton;
15 
16 public class MainActivity extends Activity implements OnClickListener{
17     private RadioButton rb1,rb2,rb3,rb4;
18     private FragmentManager mfm=null;
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         home1();
24         initView();
25     }
26 
27     private void initView() {
28         rb1=(RadioButton)findViewById(R.id.rb1);
29         rb2=(RadioButton)findViewById(R.id.rb2);
30         rb3=(RadioButton)findViewById(R.id.rb3);
31         rb4=(RadioButton)findViewById(R.id.rb4);
32         
33         rb1.setOnClickListener(this);
34         rb2.setOnClickListener(this);
35         rb3.setOnClickListener(this);
36         rb4.setOnClickListener(this);
37         
38         rb1.setChecked(true);//默认加载第一个frament
39 
40     }
41 
42     @Override
43     public void onClick(View v) {
44         switch (v.getId()) {
45         case R.id.rb1:
46             home1();
47             break;
48         case R.id.rb2:
49             home2();
50             break;
51         case R.id.rb3:
52             
53             break;
54         case R.id.rb4:
55             
56             break;
57 
58         default:
59             break;
60         }
61     }
62     private void home1()
63     {
64         Fragment f = new Home1Activity();
65         if (null == mfm)
66             mfm = getFragmentManager();
67         FragmentTransaction ft = mfm.beginTransaction();
68         ft.replace(R.id.linearlayout, f);
69         ft.commit();
70     }
71     private void home2()
72     {
73         Fragment f = new Home2Activity();
74         if (null == mfm)
75             mfm = getFragmentManager();
76         FragmentTransaction ft = mfm.beginTransaction();
77         ft.replace(R.id.linearlayout, f);
78         ft.commit();
79     }
80 
81     
82 }








Home1Activity.java
package com.example.awenapp;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Home1Activity extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.activity_home1, null);
        initview(v);
        return v;
    }

    private void initview(View v) {
        
    }
    
}

Home1Activity.java

package com.example.awenapp;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Home2Activity extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.activity_home2, null);
        initview(v);
        return v;
    }

    private void initview(View v) {
        
    }
    
}

activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7     
 8     <LinearLayout
 9         android:id="@+id/linearlayout"
10     android:orientation="vertical"
11     android:layout_width="match_parent"
12     android:layout_height="0dp"
13     android:layout_weight="1">
14         
15     </LinearLayout>
16         
17     
18     <View android:layout_width="match_parent"
19         android:layout_height="1dp"
20         android:layout_above="@+id/rg"
21         android:background="@android:color/darker_gray"
22         />
23     <RadioGroup 
24         android:layout_marginTop="3dp"
25         android:id="@+id/rg"
26         android:gravity="center_vertical"
27         android:orientation="horizontal"
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content">
30         
31         <RadioButton 
32             android:id="@+id/rb1"
33         style="@style/MenuBtn"
34         android:button="@null"
35         android:drawableTop="@drawable/selector_btn_01"/>
36         
37         <RadioButton 
38             android:id="@+id/rb2"
39         style="@style/MenuBtn"
40         android:button="@null"
41         android:drawableTop="@drawable/selector_btn_02"/>
42         
43         <RadioButton 
44             android:id="@+id/rb3"
45         style="@style/MenuBtn"
46         android:button="@null"
47         android:drawableTop="@drawable/selector_btn_03"/>
48         
49         <RadioButton
50             android:id="@+id/rb4"
51         style="@style/MenuBtn"
52         android:button="@null"
53         android:drawableTop="@drawable/selector_btn_04" />
54     </RadioGroup>
55 
56 </LinearLayout>

  

activity_home1.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7     <RelativeLayout
 8         android:layout_width="match_parent"
 9         android:background="@android:color/holo_red_light"
10         android:layout_height="0dp"
11         android:layout_weight="0.2">
12          <TextView
13             android:id="@+id/back"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:layout_centerVertical="true"
17             android:layout_marginLeft="15dip"
18             android:drawablePadding="5dip"
19             android:gravity="center"
20             android:drawableLeft="@drawable/loan_4"
21             android:textColor="#ffffff"
22             android:text="@string/back"/>
23         <TextView
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_centerHorizontal="true"
27             android:layout_centerVertical="true"
28             android:gravity="center"
29             android:textSize="20dp"
30             android:textColor="#ffffff"
31             android:text="@string/main"/>
32 
33        
34     </RelativeLayout>
35     <TextView
36         android:id="@+id/tv"
37         android:layout_width="match_parent"
38         android:layout_height="0dp"
39         android:layout_weight="0.8"
40         android:gravity="center"
41         android:text="@string/hello_world1" />
42 
43 </LinearLayout>

activity_home2.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7     <RelativeLayout
 8         android:layout_width="match_parent"
 9         android:background="@android:color/holo_red_light"
10         android:layout_height="0dp"
11         android:layout_weight="0.2">
12          <TextView
13             android:id="@+id/back"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:layout_centerVertical="true"
17             android:layout_marginLeft="15dip"
18             android:drawablePadding="5dip"
19             android:gravity="center"
20             android:drawableLeft="@drawable/loan_4"
21             android:textColor="#ffffff"
22             android:text="@string/back"/>
23         <TextView
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_centerHorizontal="true"
27             android:layout_centerVertical="true"
28             android:gravity="center"
29             android:textSize="20dp"
30             android:textColor="#ffffff"
31             android:text="@string/main"/>
32 
33     </RelativeLayout>
34    
35     <TextView
36         android:id="@+id/tv"
37         android:layout_width="match_parent"
38         android:layout_height="0dp"
39         android:layout_weight="0.8"
40         android:gravity="center"
41         android:text="@string/hello_world2" />
42 
43 </LinearLayout>

selector_btn_01.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3     <item android:state_checked="true" android:drawable="@drawable/foot_5"></item>
4     <item android:state_checked="false" android:drawable="@drawable/foot_1"></item>
5 </selector>

selector_btn_02.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3     <item android:state_checked="true" android:drawable="@drawable/huodong2"></item>
4     <item android:state_checked="false" android:drawable="@drawable/huodong1"></item>
5 </selector>

selector_btn_03.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3     <item android:state_checked="true" android:drawable="@drawable/foot_7"></item>
4     <item android:state_checked="false" android:drawable="@drawable/foot_3"></item>
5 </selector>

selector_btn_04.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3     <item android:state_checked="true" android:drawable="@drawable/foot_8"></item>
4     <item android:state_checked="false" android:drawable="@drawable/foot_4"></item>
5 
6 </selector>

styles.xml

 1 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 2 
 3     <style name="AppBaseTheme" parent="android:Theme.Light">
 4     </style>
 5     <style name="AppTheme" parent="AppBaseTheme">
 6     </style>
 7 
 8    <style name="MenuBtn">
 9        <item name="android:layout_width">0dp</item>
10        <item name="android:layout_weight">1</item> 
11        <item name="android:layout_height">65dp</item>
12        
13    </style>
14     
15     
16 </resources>



原文地址:https://www.cnblogs.com/awandxx/p/5284094.html