我创建了一个自定义工具栏,该工具栏的左侧有一个“取消”按钮,而写入时有一个“发布”按钮,这是大多数社交媒体应用程序使用的标准格式。
这是我的xml代码的一部分:
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/BarTheme"
app:theme="@style/BarTheme"
app:popuptheme="@style/Apptheme"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/white"
app:title="Lend"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add_item_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:textSize="15dp"
android:fontFamily="sans-serif-condensed-light"
android:background="@null"
android:textColor="@color/grey"
android:text="CANCEL"></Button>
<TextView
android:id="@+id/add_item_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed-light"
android:layout_gravity="bottom"
android:textStyle="bold"
android:gravity="center|center_vertical"
android:textSize="30dp"
android:text="NEW ITEM"></TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:layout_weight="1"
android:background="@null"
android:fontFamily="sans-serif-condensed-light"
android:textSize="15dp"
android:layout_marginRight="16dp"
android:textColor="@color/themeBlue"
android:textStyle="bold"
android:text="POST"></Button>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
这是我的style.xml代码:
<!-- Base application theme. -->
<style name="Apptheme" parent="Theme.AppCompat.Light.NoactionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/white</item>
<item name="colorPrimaryDark">@color/white</item>
<item name="coloraccent">@color/themeBlue</item>
</style>
<style name="BarTheme" parent="Theme.AppCompat.Light.DarkactionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/white</item>
<item name="colorPrimaryDark">@color/white</item>
<item name="coloraccent">@color/themeBlue</item>
</style>
</resources>
现在,在活动中,我决定至少尝试使用“取消”按钮,但此按钮不起作用。我尝试调试,但是它甚至都没有进入功能,因此我认为它甚至不可单击。
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.add_item_cancel:
Intent goBackintent = new Intent(AddItemactivity.this,Listingsactivity.class);
startactivity(goBackintent);
}
}
提前感谢您的帮助!