【问题标题】:Vertical LinearLayout gravity not working as spected垂直线性布局重力未按预期工作
【发布时间】:2013-09-27 01:38:47
【问题描述】:

我试图在 LinearLayout 上设置重力 =“bottom”,因此它的子视图堆叠在底部。

问题是,当我在 Eclipse 上检查它时,它看起来很好,但是当我在 AVD 上运行时,它就不起作用了。它就像重力被设置为顶部一样工作。

这是我的代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" >

<com.pepotegames.spotthedifferences.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:weightSum="4" >

    <ImageView
        android:id="@+id/stars"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/stars"
        android:background="#55000000"
        android:adjustViewBounds="true" />

</LinearLayout>

这是它在 Eclipse 中的外观,以及它应该如何工作:

http://i.imgur.com/ddHJK5S.png

编辑:顺便说一句,我的 SquareImageView 它只是一个扩展的 ImageView。问题与它无关,我已经用普通的 ImageView 测试了相同的布局,并且结果相同。

如果你想检查它,这里是:

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}

【问题讨论】:

  • 是 FrameLayout 必须为您的布局,如果现在您应该切换到 RelativeLayout 以获得更好的对齐。
  • 其实是这样,因为我需要星星来渲染图像上方。在这里你可以看到一个最终外观的示例:puu.sh/4kKq9.jpg 这个 Layout 在 GridView 内部使用

标签: android eclipse android-linearlayout layout-gravity


【解决方案1】:
  • 尝试使用相对布局而不是线性布局

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4" >
    
    <ImageView
        android:id="@+id/stars"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:background="#55000000"
        android:src="@drawable/ic_launcher" /></RelativeLayout>
    

【讨论】:

  • 我认为你不能在 RelativeLayout 中使用权重。 Eclipse 指出“RelativeLayout 中的布局参数无效:layout_weight。如果我不尝试让星星占据图像的 1/4,我可以使用 RelativeLayout,但事实并非如此。
【解决方案2】:

我无法以这种方式解决它,所以这就是我最终要做的事情

<com.pepotegames.spotthedifferences.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_container_rounded" >

    <com.pepotegames.spotthedifferences.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <com.pepotegames.spotthedifferences.QuarterImageView
        android:id="@+id/stars"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:background="@color/trans_mask_mid" />

</FrameLayout>

我做了一个平方根布局,然后我使用了另一个自定义视图,一个扩展的 ImageView,它被调整到其父高度的四分之一

public class QuarterImageView extends ImageView {
public QuarterImageView(Context context) {
    super(context);
}

public QuarterImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public QuarterImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(),getMeasuredHeight()/4);
}}

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多