【问题标题】:Aligning Views Within a ViewFlipper在 ViewFlipper 中对齐视图
【发布时间】:2012-11-07 21:12:53
【问题描述】:

我有一个 ViewFlipper 可以在 2 个视图之间翻转,一个是 ImageView,一个是 TextView。
图像和文本的大小取决于我从服务器检索到的内容。

我希望 TextView 始终具有与 ImageView 相同的高度尺寸,以便在翻转 ImageView 时 TextView 布局的大小不会改变,即使与 TextView 关联的文本很少;如果有很多文本,如果 TextView 的高度超过 ImageView,我想做一个 ellipsize="end"。

以下是我在布局中使用的代码:

<ViewFlipper
    android:id="@+id/flipperZoneMediumLeft"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imgZonePic"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/stock_service1"
        android:background="@android:color/black"
        android:scaleType="center" />

    <TextView
        android:id="@+id/txtZoneNameFlipperLeft"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center"
        android:text="This is a description"
        android:background="@android:color/black"
        android:textColor="@android:color/white" />
</ViewFlipper>

我觉得这个问题有一个简单的解决方案,但我在这条路上挣扎太久了。任何帮助解决我的问题将不胜感激。

【问题讨论】:

    标签: android android-layout viewflipper


    【解决方案1】:

    ImageViewTextView 包裹在LinearLayout 中,这样您就可以对它们施加权重。将它们的高度设置为 0dp 并给它们每个 layout_weight 1。这样它们将始终具有相同的高度。像这样:

    <ViewFlipper
        android:id="@+id/flipperZoneMediumLeft"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >
    
                <ImageView
                    android:id="@+id/imgZonePic"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@android:color/black"
                    android:scaleType="center"
                    android:src="@drawable/stock_service1" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >
    
                <TextView
                    android:id="@+id/txtZoneNameFlipperLeft"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@android:color/black"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:text="This is a description"
                    android:textColor="@android:color/white" />
            </LinearLayout>
        </LinearLayout>
    
    </ViewFlipper>
    

    【讨论】:

    • 您需要将每个视图包装在它自己的LinearLayout 中,否则您只会得到一个子ViewFlipper,而ImageViewTextView 的高度相同,只有LinearLayout .
    • 不幸的是,这并没有解决我的问题,viewflipper 不需要有两个子视图才能使用上面的布局正常工作,只需将我的 textview 放在 imageview 下方
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多