java如何确定Textview在RelativeLayout中的位置
我有一个问题,我想修复布局的位置。我想把它固定在布局的中心,但每次我点击EditText
就会弹出一个键盘,TextView
就会向上移动,我不希望发生这种情况。这是我的代码:
评论。xml:
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.design.widget.CoordinatorLayout
xmlns:flatui="http://schemas.安卓.com/apk/res-auto"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:fresco="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:fitsSystemWindows="true"
安卓:id="@+id/comments_coordinator_layout">
<安卓.support.design.widget.AppBarLayout
安卓:layout_height="wrap_content"
安卓:layout_width="match_parent"
安卓:theme="@style/AppTheme.AppBarOverlay">
<安卓.support.v7.widget.Toolbar
安卓:id="@+id/comments_appbar"
安卓:layout_width="match_parent"
安卓:layout_height="?attr/actionBarSize"
安卓:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</安卓.support.design.widget.AppBarLayout>
<RelativeLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/layout_comments">
<LinearLayout
安卓:id="@+id/send_message"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:padding="4dp"
安卓:layout_alignParentBottom="true"
安卓:orientation="horizontal" >
<com.cengalabs.flatui.views.FlatEditText
安卓:fontFamily="sans-serif"
安卓:id="@+id/write_comment"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="5"
安卓:paddingTop="6dp"
安卓:paddingBottom="6dp"
安卓:paddingLeft="5dp"
安卓:paddingRight="5dp"
安卓:gravity="left"
安卓:textSize="16sp"
flatui:fl_theme="@array/color_primary_theme"
安卓:textColor="#000000"
安卓:cursorVisible="false"
安卓:hint="Comment back!"
安卓:background="@color/feed_bg"
安卓:inputType="textMultiLine"
flatui:fl_fieldStyle="fl_box"
安卓:scrollHorizontally="false" />
<com.cengalabs.flatui.views.FlatButton
安卓:fontFamily="sans-serif"
安卓:id="@+id/send_comment"
安卓:layout_width="wrap_content"
安卓:layout_height="fill_parent"
安卓:layout_marginLeft="5dp"
安卓:textSize="16sp"
安卓:padding="5dp"
安卓:layout_gravity="center_vertical|center_horizontal"
安卓:text="Send"
flatui:fl_theme="@array/color_primary_theme"
安卓:textAllCaps="false"
flatui:fl_textAppearance="fl_light"/>
</LinearLayout>
<安卓.support.v7.widget.RecyclerView
安卓:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_above="@id/send_message"
安卓:id="@+id/view_comments">
</安卓.support.v7.widget.RecyclerView>
<TextView
安卓:id="@+id/no_comments_text"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center_horizontal|center_vertical"
安卓:visibility="gone"
安卓:textSize="16sp"
安卓:fontFamily="sans-serif"
安卓:text="No comments to display." />
</RelativeLayout>
</安卓.support.design.widget.CoordinatorLayout>
评论。爪哇:
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 安卓.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
安卓:name=".com.tabs.activity.Comments"
安卓:label="View Post"
安卓:configChanges="orientation|keyboardHidden"
安卓:windowSoftInputMode="stateAlwaysHidden|adjustPan"
安卓:theme="@style/AppTheme.NoActionBar" >
<meta-data
安卓:name="安卓.support.PARENT_ACTIVITY"
安卓:value=".com.tabs.activity.Comments"
安卓:configChanges="orientation|keyboardHidden" />
</activity>
我试图将我的TextView
设置为安卓:gravity="center_horizontal|center_vertical"
或试图设置安卓:centerVertical="true"
,但前者产生相同的行为,而后者甚至使我的TextView
不出现。有没有办法确定TextView
的位置?任何帮助都将不胜感激,谢谢
# 1 楼答案
您应该阅读有关
android:windowSoftInputMode
属性的内容,您可以将该属性添加到AndroidManifest中的活动中。xml。你可以在这里阅读:http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft你有很多选择,选择一个适合你需要的
# 2 楼答案
我认为
configChanges
标志应该被添加到你的清单文件中,以解决这个问题有关更多详细信息,请阅读开发者网站上的这篇文章
http://developer.android.com/guide/topics/resources/runtime-changes.html
# 3 楼答案
你试过:
android:layout_centerInParent="true"
用于你的TextView
,并使用android:layout_above="@+id/view_comments"
未测试,但
TextView
需要一些与RelativeLayout
相关的布局参数