[英]Android toolbar custom layout element click event
我為項目創建了一個自定義工具欄。 工具欄工作正常並顯示。 這是工具欄的代碼
工具欄
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="8"
android:weightSum="8">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/imageToolbal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/alert_on"
android:layout_weight="4"/>
<TextView
android:id="@+id/textToolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:layout_weight="4"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
主要活動的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.app.MainActivity">
<include layout="@layout/toolbar" />
</RelativeLayout>
在我的代碼的onCreate
方法中,我像這樣設置了工具欄
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
它可以正常顯示,但我無法觸發工具欄布局的click事件。 就像我想顯示Toast
如果單擊textView
id為工具欄中的textToolbar
,或者我想隱藏和顯示它有條件地。 我該怎么做 ?
這樣的工具欄操作。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setBackgroundColor(getResources().getColor(
R.color.themeToolbarColor));
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// finish();
}
});
toolbar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
您可以按以下方式獲取工具欄的文本視圖:
TextView textToolbar= (TextView)toolbar.findViewById(R.id.textToolbar);
textToolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// YOUR TOAST HERE
}
});
並隱藏並顯示如下:
if(YOUR_CONDITION){
textToolbar.setVisibility(View.VISIBLE);
}else{
textToolbar.setVisibility(View.GONE);
}
您可以為要單擊的textView
分配一個xml
onClick
事件,然后在Activity
實現它。
xml
就像
<TextView
android:id="@+id/textToolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:onclick="test"
android:layout_weight="4"/>
實現onclick事件的方法是
public void test(View V){
//onclick event statement
}
//更改“ res / styles.xml”
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
//活動中
public class MainActivity extends AppCompatActivity {
Toolbar mToolbar;
TextView txtToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolBar);
//textView inside toolbar
txtToolbar = (TextView) findViewById(R.id.txtToolbar);
setSupportActionBar(mToolbar);
}
@Override
protected void onResume() {
super.onResume();
txtToolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Toolbar Item","TxtClicked");
}
});
}
}
//工具欄
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToolBar TextView"/>
</android.support.v7.widget.Toolbar>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.