我自定义了默认标题栏,它在 2.x、3.x 设备中按预期显示,但在 4.x 设备中不显示。
在设备 2.2 上:
在设备 4.1.2 上:
这是onCreate中的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView localTextView = (TextView) getWindow().findViewById(
android.R.id.title);
if (localTextView != null) {
ViewParent localViewParent = localTextView.getParent();
if ((localViewParent != null)
&& ((localViewParent instanceof FrameLayout))) {
View localView = ((LayoutInflater) getSystemService("layout_inflater"))
.inflate(R.layout.logout_button, null);
UIImageButton localUIImageButton = (UIImageButton) localView
.findViewById(R.id.logoutButton);
Rect localRect = new Rect();
Window localWindow = getWindow();
localWindow.getDecorView().getWindowVisibleDisplayFrame(
localRect);
int i = localRect.top;
int j = localWindow.findViewById(android.R.id.title).getTop() - i;
PrintStream localPrintStream = System.out;
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = Integer.valueOf(j);
localPrintStream.printf("%d", arrayOfObject);
localUIImageButton.setMaxHeight(j);
((FrameLayout) localViewParent).addView(localView);
}
}
}
这是 list 代码:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我希望 4.x 设备中的标题栏与 2.x 中的类似。
最佳答案
Android 4.0及以上版本需要自定义action bar。参见:http://developer.android.com/guide/topics/ui/actionbar.html
类似的东西。
ActionBar actionBar = getSupportActionBar();
LayoutInflater inflator = (LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.logout_button, null); // your logout button
actionBar.setCustomView(v);
关于Android 自定义默认标题栏未在 4.x 设备中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15557526/