ContextMenu
2019. 10. 21. 15:23
Programing/Android
* 콘텍스트메뉴 기본 사용
1. ActivityMain
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="배경색 변경" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼 변경" />
</LinearLayout>
2.MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout baseLayout;
Button button1, button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("배경색 바꾸기(컨텍스트 메뉴)");
baseLayout = (LinearLayout) findViewById(R.id.baseLayout);
button1 = (Button) findViewById(R.id.button1);
registerForContextMenu(button1);
button2 = (Button) findViewById(R.id.button2);
registerForContextMenu(button2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mInflater = getMenuInflater();
if (v == button1) {
menu.setHeaderTitle("배경색 변경");
mInflater.inflate(R.menu.menu1, menu);
}
if (v == button2) {
mInflater.inflate(R.menu.menu2, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.itemRed:
baseLayout.setBackgroundColor(Color.RED);
return true;
case R.id.itemGreen:
baseLayout.setBackgroundColor(Color.GREEN);
return true;
case R.id.itemBlue:
baseLayout.setBackgroundColor(Color.BLUE);
return true;
case R.id.subRotate:
button2.setRotation(45);
return true;
case R.id.subSize:
button2.setScaleX(2);
return true;
}
return false;
}
}
3. menu1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/itemRed"
android:title="배경색 (빨강)">
</item>
<item
android:id="@+id/itemGreen"
android:title="배경색(초록)">
</item>
<item
android:id="@+id/itemBlue"
android:title="배경색(파랑)">
</item>
</menu>
4.menu2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/subRotate"
android:title="버튼 45도 회전"/>
<item
android:id="@+id/subSize"
android:title="버튼 2배 확대"/>
</menu>
* Item에 라디오 버튼 적용
위 내용에 item xml 수정
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" >
<group
android:checkableBehavior="single">
<item
android:id="@+id/itemRed"
android:title="배경색 (빨강)"></item>
<item
android:id="@+id/itemGreen"
android:title="배경색(초록)">
</item>
<item
android:id="@+id/itemBlue"
android:title="배경색(파랑)">
</item>
</group>
</menu>
'Programing > Android' 카테고리의 다른 글
커스텀 토스트, 커스텀 다이얼로그 (0) | 2019.10.23 |
---|---|
배열과 테이블 위젯 이용한 계산기, 숫자버튼 예제 (0) | 2019.10.21 |
탭호스트 (0) | 2019.10.21 |
날짜 시간 위젯 (0) | 2019.10.21 |
Android LineChart (0) | 2019.10.16 |