custom_actionbar.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:background="#2F9D27"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ImageButton
        android:id="@+id/btnBack"
        android:layout_alignParentLeft="true"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:background="@drawable/top_back"/>
 
    <TextView
        android:text="타이틀"
        android:id="@+id/title"
        android:textSize="20sp"
        android:textColor="#fff"
        android:gravity="center_vertical"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@id/btnBack"
        android:layout_width="match_parent"
        android:layout_height="56dp" />
 
    <ImageButton
        android:id="@+id/btnHistory"
        android:layout_alignParentRight="true"
        android:background="@drawable/top_history"
        android:layout_width="56dp"
        android:layout_height="56dp" />
</RelativeLayout>
cs

RelativeLayout을 이용하여 Custom한다



MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //커스텀 액션바 만들기
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ActionBar actionBar = getSupportActionBar();
 
        // Custom Actionbar를 사용하기 위해 CustomEnabled을 true 시키고 필요 없는 것은 false 시킨다
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);            //액션바 아이콘을 업 네비게이션 형태로 표시합니다.
        actionBar.setDisplayShowTitleEnabled(false);        //액션바에 표시되는 제목의 표시유무를 설정합니다.
        actionBar.setDisplayShowHomeEnabled(false);            //홈 아이콘을 숨김처리합니다.
 
 
        //layout을 가지고 와서 actionbar에 포팅을 시킵니다.
        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View actionbar = inflater.inflate(R.layout.custom_actionbar, null);
 
        actionBar.setCustomView(actionbar);
 
        //액션바 양쪽 공백 없애기
        Toolbar parent = (Toolbar)actionbar.getParent();
        parent.setContentInsetsAbsolute(0,0);
 
        return true;
    }
}
cs

onCreateOptionsMenu를 override한다



결과


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class MainActivity extends AppCompatActivity {
 
    //GPS
    LocationManager locationManager;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //GPS ON/OFF 확인해서 OFF이면 GPS 설정화면으로 이동하기
 
 
        //LocationManager
        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    }
 
    //버튼
    public void mOnGPSClick(View v){
        //GPS가 켜져있는지 체크
        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            //GPS 설정화면으로 이동
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }
    }
}
cs



AndroidMenifest.xml

1
2
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
cs



결과

 


팝업 바깥레이어 클릭시 닫히지 않게 하기
1
2
3
4
5
6
7
8
9
10
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //바깥레이어 클릭시 안닫히게
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
            return false;
        }
        return true;
    }
 
 
cs


+ Recent posts