我有一个活动,需要在旋转后更改其布局,其中的一部分布局是使用视图的宽度和高度绘制的图形。当代码第一次运行时,图形会被正确地绘制出来,但旋转后,容器视图的宽度和高度不正确,实际上它们似乎是未旋转的视图。
到目前为止,我所做的如下:
- 在我正在工作的活动的清单中: ``` android:configChanges="keyboardHidden|orientation|screenSize" ```
- 在我的活动中,我有以下方法: ``` onCreate ```
旋转后,在layoutGraph()中,我遇到了问题,在这里viewWidth和viewHeight对象。在那个时候,我假设(使用全局布局监听器)值是正确的。我的理解是,监听器只会在“graph_container”完成(横向或纵向)后触发一次,因此调用layoutGraph()时,“mainGraph_layout”的宽度和高度(一个子图形容器,宽度和高度设置为MATCH_PARENT)将已经准备就绪。看起来我所得到的宽度和高度都像手机仍处于纵向状态,需要注意的是操作栏被移除也已经考虑进去了。
抱歉问题有点长,但我认为展示所有的代码最好。如果还需要展示其他内容,请告诉我。
提前感谢, Josh
到目前为止,我所做的如下:
- 在我正在工作的活动的清单中: ``` android:configChanges="keyboardHidden|orientation|screenSize" ```
- 在我的活动中,我有以下方法: ``` onCreate ```
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
patient_id = extras.getInt("patient_id");
patient_name = extras.getString("patient_name");
historyDurationType = 12;
constructLayout();
}
构建布局
public void constructLayout(){
if(landScape){
setContentView(R.layout.activity_bg_history_static_land);
//Set buttons
btnTwelve = (Button)findViewById(R.id.btnTwelveHoursLand);
btnTwentyFour = (Button)findViewById(R.id.btnTwentyFourHoursLand);
btnSeven= (Button)findViewById(R.id.btnSevenDaysLand);
btnTwelve.setOnClickListener(this);
btnTwentyFour.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnTwelve.setBackgroundColor(getResources().getColor(R.color.light_blue_regular));
btnTwentyFour.setBackgroundResource(android.R.drawable.btn_default);
btnSeven.setBackgroundResource(android.R.drawable.btn_default);
}else{
setContentView(R.layout.activity_bg_history_static);
//Set buttons
btnTwelve = (Button)findViewById(R.id.btnTwelveHours);
btnTwentyFour = (Button)findViewById(R.id.btnTwentyFourHours);
btnSeven= (Button)findViewById(R.id.btnSevenDays);
btnTwelve.setOnClickListener(this);
btnTwentyFour.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnTwelve.setBackgroundColor(getResources().getColor(R.color.light_blue_regular));
btnTwentyFour.setBackgroundResource(android.R.drawable.btn_default);
btnSeven.setBackgroundResource(android.R.drawable.btn_default);
btnComment = (Button)findViewById(R.id.btnCommentGraph);
btnComment.setOnClickListener(this);
populateOtherContent(officialReadings12);
TextView tvStats = (TextView)findViewById(R.id.txtStatistics);
Typeface chunkFiveFont = Typeface.createFromAsset(getAssets(), "fonts/chunkfivettfversion.ttf");
tvStats.setTypeface(chunkFiveFont);
TextView tvReading = (TextView)findViewById(R.id.txtReadingTitle);
tvReading.setTypeface(chunkFiveFont);
comment = null;
}
if(needData){
getLatestReadings();
}
populateGraph();
}
populateGraph
public void populateGraph(){
if(landScape){
graph_container = (LinearLayout)findViewById(R.id.graph_land_content_layout);
}else{
graph_container = (LinearLayout)findViewById(R.id.graph_content_layout);
}
//Create graphlayout
mainGraph_Layout = new RelativeLayout(this);
RelativeLayout.LayoutParams glParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mainGraph_Layout.setId(909);
mainGraph_Layout.setLayoutParams(glParams);
graph_container.addView(mainGraph_Layout);
graph_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if(needsGraph){
layoutGraph();
needsGraph = false;
}
}
});
}
布局图
public void layoutGraph(){
viewWidth = mainGraph_Layout.getWidth();
viewHeight = mainGraph_Layout.getHeight();
//MORE STUFF IS HERE BUT NOT IMPORTANT
}
onConfigurationChanged
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ActionBar actionBar = getActionBar();
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//Config is landscape here
actionBar.hide();
needData = false;
landScape = true;
needsGraph = true;
constructLayout();
}else{
//Config is portrait here
actionBar.show();
needData = false;
landScape = false;
needsGraph = true;
constructLayout();
}
}
旋转后,在layoutGraph()中,我遇到了问题,在这里viewWidth和viewHeight对象。在那个时候,我假设(使用全局布局监听器)值是正确的。我的理解是,监听器只会在“graph_container”完成(横向或纵向)后触发一次,因此调用layoutGraph()时,“mainGraph_layout”的宽度和高度(一个子图形容器,宽度和高度设置为MATCH_PARENT)将已经准备就绪。看起来我所得到的宽度和高度都像手机仍处于纵向状态,需要注意的是操作栏被移除也已经考虑进去了。
抱歉问题有点长,但我认为展示所有的代码最好。如果还需要展示其他内容,请告诉我。
提前感谢, Josh