Android自定义标题栏视图未正确对齐

时间:2013-09-28 22:22:12

标签: android eclipse

我有一个自定义标题栏。这是风格部分。

<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>

这是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>

这是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);

}

以下是它的显示方式:

http://imgur.com/0y5SANf

我的问题是那两个imageviews(卫星)应该与左边和右边对齐。我似乎无法实现这一点。

有没有人知道为什么他们没有对齐?

它在Eclipse的图形布局中正确显示,但是当我在模拟器和物理设备上运行时,它会像图片一样显示。我应该提一下我的活动扩展CustomTitle。

1 个答案:

答案 0 :(得分:0)

从图片视图和linearlayout中删除layout_weight。也使用重力而不是layout_gravity。我想(我目前没有自己的电脑)这可以解决你的问题。您可以使用的另一个路径是使用相对布局作为根布局而不是第一个linearlayout,并在图像视图和第二个linearlayout上使用alignParentLeft和alignParentRight。