Androidのカスタムタイトルバー(コンビネーションコントロール)
8148 ワード
簡単なカスタムタイトルバー
Androidカスタムコントロールは開発者が最も頭を悩ませてきたが、私たちはそのような困難を迎える精神を持っていなければならない.
MainActivity
TitleLayout.class
activity_main.xml
custom_layout.xml
以上のコードを貼り付けると実行できます.
Androidカスタムコントロールは開発者が最も頭を悩ませてきたが、私たちはそのような困難を迎える精神を持っていなければならない.
MainActivity
package com.example.customview;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/*
android
:
1. xml, ?
linearlayout
2. xml
3. layoutInflat
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}
TitleLayout.class
package com.example.customview.custom;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.customview.R;
/**
*
*/
public class TitleLayout extends LinearLayout implements View.OnClickListener {
private Button btback, btopen;
private TextView tvtitle;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//
LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
initView();
}
private void initView() {//
btback = (Button) findViewById(R.id.btback);
btback.setOnClickListener(this);
btopen = (Button) findViewById(R.id.btopen);
btopen.setOnClickListener(this);
tvtitle = (TextView) findViewById(R.id.tvtitle);
tvtitle.setOnClickListener(this);
}
@Override
public void onClick(View view) {//
switch (view.getId()) {
case R.id.btback:
((Activity) getContext()).finish();
Toast.makeText(getContext(), " Activity", Toast.LENGTH_SHORT).show();
break;
case R.id.btopen:
Toast.makeText(getContext(), " ", Toast.LENGTH_SHORT).show();
break;
case R.id.tvtitle:
Toast.makeText(getContext(), " ", Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.customview.MainActivity">
<include layout="@layout/custom_layout" />
<com.example.customview.custom.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
custom_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.customview.MainActivity">
<include layout="@layout/custom_layout" />
<com.example.customview.custom.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
以上のコードを貼り付けると実行できます.