所以我试图做一个类似Facebook的应用程序和许多其他人的动作栏。操作栏具有一个功能,您可以在其中键入编辑文本,然后按保存按钮。但到目前为止,我的尝试失败了。布局是使用app:actionLayout的menu.xml,它指的是我的项目布局。但是当我将它放在OnCreate下时应用程序崩溃,并且在OnCreateOptionsMenu下调用时它也不起作用。有人可以帮忙吗?
这是布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageButton
android:layout_width="57dp"
android:layout_height="48dp"
android:id="@+id/postButton"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_send_now"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editMessage"
android:layout_alignBottom="@+id/postButton"
android:layout_toLeftOf="@+id/postButton"
android:layout_toStartOf="@+id/postButton"/>
</RelativeLayout>
这是menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="@+id/action_message"
app:actionLayout="@layout/message_fragment"
app:showAsAction="always|collapseActionView"
android:icon="@drawable/ic_action_chat"
android:title="@string/message_item"/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:icon="@drawable/ic_action_overflow"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
这是我在活动中的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater ().inflate (R.menu.menu_main, menu);
View view = menu.findItem(R.id.action_message).getActionView();
view.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
}
});
final EditText editMessage = (EditText) findViewById (R.id.editMessage);
final ImageButton actionSendButton = (ImageButton) findViewById (R.id.postButton);
actionSendButton.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
Post post = getCurrentPost ();
post.setPost (editMessage.getText ().toString ());
post.setGamer (ParseUser.getCurrentUser ());
post.saveInBackground (new SaveCallback () {
@Override
public void done(ParseException e) {
if (e == null) {
setResult (Activity.RESULT_OK);
finish ();
} else {
Context context = getApplicationContext();
CharSequence text = "Posting and Thank You!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText (context, text, duration).show ();
}
}
});
}
});
return true;
}
答案 0 :(得分:0)
我弄清楚它是如何失效的。我试图把它放在动作栏中,但你可以通过添加一个片段然后强制它到屏幕底部来实现。