简体   繁体   中英

Android Custom Title Bar views not aligning correctly

I have a custom title bar. Here's the style part.

<style name="CustomWindowTitleBackground">
    <item name="android:background">#323331</item>
</style>

<style name="CustomTheme" parent="android:Theme">
    <item name="android:windowTitleSize">35dip</item>
    <item name="android:padding">0px</item>
    <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    <item name="android:windowBackground">@drawable/gradient</item>
</style>

Here's the XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#323331"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_weight="1"
    android:src="@drawable/satellite" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="right">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/satellite" />

</LinearLayout>


</LinearLayout>

Here's the class for that XML

public class CustomTitle extends Activity {
protected TextView title;
protected ImageView icon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.customtitle);

    title = (TextView) findViewById(R.id.title);
    icon  = (ImageView) findViewById(R.id.icon);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitle);

}

Here's how it appears:

http://imgur.com/0y5SANf

My problem is that those two imageviews (satellites) should be aligned to the Left and to the Right. I can't seem to get that to happen.

Does anyone have an idea as to why they aren't aligning?

It appears correctly in the graphical layout in Eclipse , but when I run it in the emulator and on my physical device it shows up like the picture. I should mention my activites extend CustomTitle.

Remove the layout_weight from your image view and linearlayout. Also use gravity and not layout_gravity. I think (I don't currently have my PC on) this should solve your problem. Another route you could use is to use a relative layout as the root layout instead of your first linearlayout and use alignParentLeft and alignParentRight on your image view and second linearlayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM