【问题标题】:android add a button to an header (iOS look)android向标题添加按钮(iOS外观)
【发布时间】:2013-01-29 17:21:39
【问题描述】:

嘿,我正在尝试创建一个标题,中间有一个图像,右上角有一个按钮。

我找到了这篇文章how to make headers in android with text and buttons like in iOS,以及下面的教程http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/,我能够将图像放在中心并设置背景。但是,无论我做什么,我的按钮都不会出现在右上角。

我试图查看它是否被背景遮盖或超出了屏幕范围,但我找不到问题的原因。

我的 XML

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <ImageView
            android:id="@+id/Header"
            android:src="@drawable/header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ff000000"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"/>

                <Button
            android:id="@+id/lockButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5.0dip"        
            android:text="TEST"
            />


    </LinearLayout>

【问题讨论】:

标签: android android-layout android-button


【解决方案1】:

为什么不relative

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/img_vs_navbar" >

<ImageView
    android:id="@+id/img_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/btn_setting_back" />

</RelativeLayout>

您可以像这样使用android:layout_centerInParent 将其置于中心位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/img_vs_navbar" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/btn_setting_back" />

</RelativeLayout>

或者用alignParentRight放在右边

编辑:

这个是在一个xml中使用两个imageview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/img_vs_navbar" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/btn_setting_back" />

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/btn_setting_back" />

</RelativeLayout>

【讨论】:

  • 相对于线性的附加价值是什么?
  • 您可以根据需要自定义标题。
  • 你也会在同一个xml中定义两者吗?还是你需要分开xmls?
  • 刚刚用使用两个图像视图的新 xml 更新了答案。希望这会有所帮助
  • 9patch 是解决此类问题的好方法。 developer.android.com/tools/help/draw9patch.html
【解决方案2】:

试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/Header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#ff000000"
        android:gravity="center_horizontal"
        android:src="@drawable/header" />

    <Button
        android:id="@+id/lockButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5.0dip"
        android:text="TEST" />

</RelativeLayout>

【讨论】:

  • 嘿,这可行,问题是我的图像太大并且与按钮重叠。我认为缩放它会做
【解决方案3】:

因为你的 ImageView 有一个 fill_parent 宽度,你需要给它添加一个 weight 属性。

android:layout_weight="1"

如果您希望它跨越整行,您可以使用将 LinearLayout 更改为 RelativeLayout。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多