我遇到了这个问题,我想修复布局的位置。我想在我的布局中心修复它,但每次我点击弹出键盘的EditText
时,TextView
都会向上移动,我不希望这种情况发生。这是我的代码:
comment.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:flatui="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
android:id="@+id/comments_coordinator_layout">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/comments_appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_comments">
<LinearLayout
android:id="@+id/send_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<com.cengalabs.flatui.views.FlatEditText
android:fontFamily="sans-serif"
android:id="@+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:gravity="left"
android:textSize="16sp"
flatui:fl_theme="@array/color_primary_theme"
android:textColor="#000000"
android:cursorVisible="false"
android:hint="Comment back!"
android:background="@color/feed_bg"
android:inputType="textMultiLine"
flatui:fl_fieldStyle="fl_box"
android:scrollHorizontally="false" />
<com.cengalabs.flatui.views.FlatButton
android:fontFamily="sans-serif"
android:id="@+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:textSize="16sp"
android:padding="5dp"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Send"
flatui:fl_theme="@array/color_primary_theme"
android:textAllCaps="false"
flatui:fl_textAppearance="fl_light"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/send_message"
android:id="@+id/view_comments">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/no_comments_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:visibility="gone"
android:textSize="16sp"
android:fontFamily="sans-serif"
android:text="No comments to display." />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Comments.java:
public class Comments extends AppCompatActivity {
Post post;
private CommentsDataSource commentsDatasource;
//Local Database for storing posts
private PostsDataSource postsDataSource;
private FireBaseApplication application;
private List<Comment> commentItems;
private CommentsRecyclerViewAdapter commentsRecyclerViewAdapter;
private RecyclerView commentsView;
private TextView noCommentsView;
private Toolbar toolbar;
private LinearLayoutManager llm;
private NotificationManager notificationManager;
private boolean isNotificationActive;
private String postId;
private String tab;
private String userId;
private String posterUserId;
private String posterName;
private String postTimeStamp;
private String postStatus;
//Progress overlay
View progressOverlay;
DatabaseQuery databaseQuery;
String name;
private Firebase firebaseRef = new Firebase("https://tabsapp.firebaseio.com/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comments);
setupActionBar();
setupActivity(savedInstanceState);
final EditText comment = (EditText) findViewById(R.id.write_comment);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//Once we send the post, we want to
comment.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
comment.setCursorVisible(false);
if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
comment.setImeOptions(EditorInfo.IME_ACTION_DONE);
in.hideSoftInputFromWindow(comment.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
//Hide the cursor until view is clicked on
View.OnTouchListener onTouchListener = new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touched");
if (v.getId() == comment.getId()) {
comment.setCursorVisible(true);
}
commentsView.postDelayed(new Runnable() {
@Override
public void run() {
commentsView.smoothScrollToPosition(commentsView.getAdapter().getItemCount() - 1);
}
}, 250);
return false;
}
};
comment.setOnTouchListener(onTouchListener);
//Button for sending post
final Button button = (Button) findViewById(R.id.send_comment);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (TextUtils.isEmpty(comment.getText())) {
Toast.makeText(Comments.this, "Please enter in a comment first.", Toast.LENGTH_SHORT).show();
} else {
String text = comment.getText().toString();
Comment createdComment = new Comment("", postId, name, text, userId, getDateTime());
Toast.makeText(Comments.this, "Successfully commented.", Toast.LENGTH_SHORT).show();
comment.setText("");
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(comment.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
comment.setCursorVisible(false);
updatePost();
databaseQuery.saveCommentToFirebase(createdComment);
saveCommentInCloud(createdComment, tab);
if (noCommentsView.getVisibility() == View.VISIBLE) {
noCommentsView.setVisibility(View.GONE);
}
//Notify friends that user has posted a comment on their post. Don't get notification if you posted on your own post.
// if(!createdComment.getCommenterUserId().equals(userId)) {
// showNotification(v, commenter);
// }
}
}
});
//Now we have to show a loading bar so that we are loading the comments. While we are loading the comments, we update the comments header
populateCommentView(postId);
}
public void populateCommentView(String postId) {
commentItems = new ArrayList<Comment>();
getComments(postId);
}
private void setupActionBar() {
toolbar = (Toolbar) findViewById(R.id.comments_appbar);
setSupportActionBar(toolbar);
//Back bar enabled
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//What happens if you click back
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void updatePost(){
commentsView.postDelayed(new Runnable() {
@Override
public void run() {
commentsView.smoothScrollToPosition(commentsView.getAdapter().getItemCount());
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}, 1000);
}
private void checkAdapterIsEmpty () {
if(application.getCommentsRecyclerViewAdapter().getItemCount() == 1){
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) noCommentsView.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.view_post);
noCommentsView.setVisibility(View.VISIBLE);
}
else {
noCommentsView.setVisibility(View.GONE);
}
}
public CommentsHeader getCommentsHeader(String id)
{
System.out.println("Going to inflate header");
CommentsHeader header = new CommentsHeader();
header.setPosterUserId(posterUserId);
header.setPosterName(posterName);
header.setPosterDate(postTimeStamp);
header.setViewStatus(postStatus);
return header;
}
public void populatePost(String id) {
TextView statusMsg = (TextView)findViewById(R.id.view_status);
System.out.println("Post: " + post);
statusMsg.setText(post.getStatus());
//Set profile picture
DraweeController controller = news_feed.getImage(userId);
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.poster_picture);
draweeView.setController(controller);
//Set poster's name
TextView posterName = (TextView)findViewById(R.id.poster_name);
posterName.setText(post.getName());
//Set date of when post was created
TextView postDate = (TextView) findViewById(R.id.post_date);
postDate.setText(AndroidUtils.convertDate(post.getTimeStamp()));
}
public String getIntentString(String value){
Bundle extras = getIntent().getExtras();
String result = "";
if (extras != null) {
result = extras.getString(value);
}
return result;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putString("postId", postId);
savedInstanceState.putString("tab", tab);
savedInstanceState.putString("userId", userId);
savedInstanceState.putString("name", name);
savedInstanceState.putString("posterUserId", posterUserId);
savedInstanceState.putString("posterName", posterName);
savedInstanceState.putString("postTimeStamp", postTimeStamp);
savedInstanceState.putString("postStatus", postStatus);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
private void setupActivity(Bundle savedInstanceState) {
if (savedInstanceState != null) {
// Restore value of members from saved state
if(savedInstanceState.containsKey("tab")) {
tab = savedInstanceState.getString("tab");
}
if(savedInstanceState.containsKey("postId")) {
postId = savedInstanceState.getString("postId");
}
if(savedInstanceState.containsKey("userId")) {
userId = savedInstanceState.getString("userId");
}
if(savedInstanceState.containsKey("name")) {
name = savedInstanceState.getString("name");
}
if(savedInstanceState.containsKey("posterUserId")) {
posterUserId = savedInstanceState.getString("posterUserId");
}
if(savedInstanceState.containsKey("posterName")) {
posterName = savedInstanceState.getString("posterName");
}
if(savedInstanceState.containsKey("postTimeStamp")) {
postTimeStamp = savedInstanceState.getString("postTimeStamp");
}
if(savedInstanceState.containsKey("postStatus")) {
postStatus = savedInstanceState.getString("postStatus");
}
} else {
postId = getIntentString("postId");
tab = getIntentString("tab");
userId = getIntentString("userId");
posterUserId = getIntentString("posterUserId");
posterName = getIntentString("posterName");
postTimeStamp = getIntentString("postTimeStamp");
postStatus = getIntentString("postStatus");
// Probably initialize members with default values for a new instance
}
databaseQuery = new DatabaseQuery(this);
application = ((FireBaseApplication) getApplication());
progressOverlay = findViewById(R.id.progress_overlay);
if(application.getName() != null && application.getName() != "") {
name = application.getName();
} else {
if(savedInstanceState != null) {
if(savedInstanceState.containsKey("name")) {
name = savedInstanceState.getString("name");
}
}
}
toolbar = (Toolbar) findViewById(R.id.comments_appbar);
notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
commentsView = (RecyclerView) findViewById(R.id.view_comments);
noCommentsView = (TextView) findViewById(R.id.no_comments_text);
llm = new LinearLayoutManager(this);
commentsView.setLayoutManager(llm);
}
}
的AndroidManifest.xml:
<activity
android:name=".com.tabs.activity.Comments"
android:label="View Post"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".com.tabs.activity.Comments"
android:configChanges="orientation|keyboardHidden" />
</activity>
我尝试将TextView
设置为android:gravity="center_horizontal|center_vertical"
或尝试设置android:centerVertical="true"
,但前者产生相同的确切行为,后者使TextView
不均匀出现。有什么方法可以解决TextView
的位置吗?任何帮助将不胜感激,谢谢!
答案 0 :(得分:1)
在Manifest.xml上的活动标记中添加 android:windowSoftInputMode =&#34; adjustPan&#34;
答案 1 :(得分:0)
我认为configChanges
标志应添加到您的清单文件中,以解决此问题
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
有关详细信息,请阅读开发人员网站上的这篇文章
http://developer.android.com/guide/topics/resources/runtime-changes.html
答案 2 :(得分:0)
您应该阅读可以添加到AndroidManifest.xml中的Activity的android:windowSoftInputMode
属性。您可以在此处阅读:http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
您有很多选择,选择适合您需求的选项。
答案 3 :(得分:0)
你试过了吗?
android:layout_centerInParent="true"
代表TextView
,并使用android:layout_above="@+id/view_comments"
?
未经测试,但TextView
需要一些与RelativeLayout
相关的布局参数。