如果我有这个void,它会将多个RelativeLayouts添加到一个RelativeLayout(messages_container_view)中,然后将其添加到滚动视图(MessagesScrollingView)中,我可以看到消息,但ScrollingView默认为顶部。我希望它默认为滚动视图的底部。
滚动视图已动态创建。
我可以看到滚动视图和消息。
public void individualConversationMessages(final Context ctx){
runOnUiThread(new Runnable() {
@Override
public void run() {
//Clear the screen
MB.removeAllViews();
//Set up the screen dimensions
Display display = getWindowManager().getDefaultDisplay();
//This is where the individual conversation will get stored
JSONArray ConversationMessages = new JSONArray();
int container_id = 1;
//The messages go in this container which goes in the scrollbox
RelativeLayout messages_container_view = new RelativeLayout(ctx);
try {
//Get all conversations on the device
JSONArray Conversations = new JSONArray(SharedPrefs(ctx).getString("conversations_array", ""));
//Store only a single conversation
ConversationMessages = Conversations.getJSONArray(ConversationID);
//This is how many messages are in the conversation.
int MessageCount = ConversationMessages.length();
for(int i = 0;i<MessageCount;i++){
//Get the message we are displaying
JSONObject MessageData = ConversationMessages.getJSONObject(i);
//Message container
RelativeLayout MessageContainer = new RelativeLayout(ctx);
//Set all the messages in the conversation to read so that they no longer appear.
MessageData.put("read",1);
//This is where the message will be displayed
MessageContainer.setMinimumWidth(display.getWidth());
//This is the message information we will be displaying.
TextView message_text = new TextView(ctx);
//Set the message data
String body = "\n" + MessageData.getString("sent") + "\n" + MessageData.getString("message");
message_text.setText(body);
//Make those a nice easy to read size
message_text.setTextSize(13);
//Add the message text and sent time to the container
MessageContainer.addView(message_text);
MessageContainer.setMinimumHeight(message_text.getMeasuredHeight() + 20);
//Layout params
if(container_id != 0) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, container_id);
MessageContainer.setLayoutParams(params);
}
container_id++;
MessageContainer.setId(container_id);
//Add the message container to the view of messages
messages_container_view.addView(MessageContainer);
//Save all the messages in this conversation to red status
SharedPrefs(ctx).edit().putString("conversations_array", Conversations.toString()).commit();
}
} catch (Exception e){
TextView message = new TextView(ctx);
message.setTextSize(15);
message.setTextColor(Color.parseColor("#ff0000"));
message.setText("Error \n \n Something went wrong \n \n" + e.getClass().getSimpleName());
MB.addView(message);
e.printStackTrace();
}
//Add the messages into the scrolling view.
ScrollView MessagesScrollingView = new ScrollView(MyMessages.this);
MessagesScrollingView.addView(messages_container_view);
MessagesScrollingView.scrollTo(0, MessagesScrollingView.getBottom());
MB.addView(MessagesScrollingView);
}
});
}
正如您所看到的,我尝试使用MessagesScrollingView.scrollTo(0, MessagesScrollingView.getBottom());
,但这并没有奏效。我也试过去maxScrollAmount(),但这也没有用。