【问题标题】:Android: Bar with centered image and buttons on the rightAndroid:带有居中图像和右侧按钮的栏
【发布时间】:2011-04-07 17:56:36
【问题描述】:

我真的在为我的应用程序中的一部分布局而苦苦挣扎。我有一个带有居中图像的标题栏,右侧应该有两个按钮(或像我现在拥有的可点击图像)。我设法让图像彼此相邻(全部居中),但我想要右边的图像。当我使用 layout_weight 进行“第一个”拉伸时,按钮在右侧,但它们变得如此之小。我希望按钮(图像)保持其原始大小。我什至尝试使用 minWidth 和 minHeight 对其进行硬编码,但这不起作用。

以下是所有内容的中心代码:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/header_tile">

    <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/header_logo">
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

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

        <ImageView
           android:id="@+id/next"
           android:src="@drawable/omlaag"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        </ImageView>
    </LinearLayout>
</LinearLayout>

下面是有效的代码,只是最后一张图片变得太小了:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/header_tile">

    <ImageView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:src="@drawable/header_logo"
        android:layout_weight="2">
    </ImageView>

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

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

        <ImageView
           android:id="@+id/next"
           android:src="@drawable/omlaag"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        </ImageView>
    </LinearLayout>
</LinearLayout>

有什么想法吗?非常感谢!

埃里克

【问题讨论】:

    标签: android layout


    【解决方案1】:

    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:gravity="center">
         <Button android:text="@+id/Button03"
             android:id="@+id/Button03" 
             android:layout_height="wrap_content"
              android:layout_weight="0" 
              android:layout_width="wrap_content">
        </Button>
    </LinearLayout>
    
    <LinearLayout
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content">      
        <Button android:id="@+id/Button01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="/\">
            </Button>
        <Button android:id="@+id/Button02" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="\/">
            </Button>
    </LinearLayout>
    

    不知道图片的大小,所以我使用了按钮。权重值可以为2吗? 0 或 1 我一直在使用。

    【讨论】:

      【解决方案2】:

      layout_weight 与 fill_parent 和 wrap_content 一起使用时的工作方式不同;

      • wrap_content:尝试按权重比例展开item,覆盖整个空间

      • fill_parent:它尝试按权重比例压缩项目。

      使用权重时,所有视图必须具有相同的类型(fill_parent/wrap_content)

      【讨论】:

        【解决方案3】:

        使用相对布局,它更强大,注意 layout_centerInParent 和 layout_alignParentRight 属性:

        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/header_tile">
            <ImageView
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:src="@drawable/header_logo"
                android:layout_centerInParent="true">
            </ImageView>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true">
                <ImageView
                   android:id="@+id/previous"
                   android:src="@drawable/omhoog"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content">
                </ImageView>
                <ImageView
                   android:id="@+id/next"
                   android:src="@drawable/omlaag"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content">
                </ImageView>
            </LinearLayout>
        </RelativeLayout>
        

        【讨论】:

        • 效果很好,直到现在才知道RelativeLayout。完美!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-20
        • 2017-01-25
        • 1970-01-01
        • 2018-05-26
        • 2021-05-04
        • 2022-01-12
        相关资源
        最近更新 更多