问题
I am using AA and would like to specify two different xml layout files for portrait and landscape orientations.
In my java class, marked as EActivity a method annotated with @Click is provided to respond to click event.
Everything is working fine in portrait mode. But when the phone is rotated, the layout changes and buttons are not responding anymore.
My guess is that the layout provided after @EActivity annotation is the portrait layout.
How can I provide both portrait and landscape layouts ?
Thanks in advance.
Update I 'listen' to orientation changes using two different layout files one, for portrait which is named layout/contact.xml and the other, for landscape which is named layout-land/contact.xml. Android does the rest and uses the correct layout according to phone orientation.
To register listener for click events, I use Android Annotations:
contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent">
<include
layout="@layout/header_layout" />
<!-- Some elements -->
</LinearLayout>
header_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="60dp">
<ImageButton
android:id="@+id/menuHome"
android:layout_width="25dp"
android:src="@drawable/menu"
android:paddingTop="22dp"
android:paddingBottom="22dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:background="#00000000"
android:scaleType="fitCenter"
android:layout_height="match_parent" />
<!-- Other buttons -->
</LinearLayout>
contact.java
@EActivity(R.layout.activity_contact)
public class ContactActivity extends TopLevelActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public String getPageTitle() {
return "Nous contacter";
}
}
** TopLevelActivity.java **
@EActivity
public abstract class TopLevelActivity extends Activity implements ITopLevelActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Click
public void menuHome () {
startActivity(new Intent(this, HomeActivity_.class));
}
}
回答1:
Add the following code in your java file
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.activity_main_h);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_main_v);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
}
Also, add the following to your activity in the Manifest:
android:configChanges="keyboardHidden|orientation|screenSize"
回答2:
In order to create different layout configurations for portrait or landscape orientation preserving single resource file name that will be picked up automatically by the system you can use configuration qualifiers.
Configuration qualifiers used to determine which resource should be used. We have multiple qualifiers in Android system: "Table 2. Configuration qualifier names."
Here are sample screenshots to explain how you pick desired configuration qualifiers.
1) First, you enter resource name and select a qualifier from "Available qualifiers" list;
2) Press [>>] button to move selected qualifier to the list of "Chosen qualifiers"; 3) Now you have qualifier selected and can specify value for each individual qualifier.
Note: you cannot select one type of qualifier only once per file. E.g. one size qualifier, one density qualifier, etc.
In "Project View" tab under app/src/main/res
you can see next structure:
1) In "Android view" mode:
2) In "Project Files" mode
EXTREMELY IMPORTANT
You should provide default version of resource file under default folders like layout
, drawable
, values
etc., if you have resource files that depend on configuration qualifiers. If configuration qualifiers are not met, meaning device running your application does not qualify to use given resources, and your application does not have default resource file your application will crash with
android.content.res.Resources$NotFoundException: Resource ID #0x7f0b001c
来源:https://stackoverflow.com/questions/32246147/android-annotations-different-layout-for-portrait-and-landscape