垂直LinearLayout重力不按预期工作

时间:2013-09-06 21:28:58

标签: android eclipse android-linearlayout layout-gravity

我试图设置引力="底部"在LinearLayout上,所以它的子视图堆栈在底部。

问题在于,当我在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());
}
}

2 个答案:

答案 0 :(得分:0)

  • 尝试使用相对布局而不是线性布局

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

答案 1 :(得分:0)

我无法解决这个问题,所以这就是我最终做的事情

<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);
}}