【问题标题】:Android LinearLayout margin - unable to adjustAndroid LinearLayout 边距 - 无法调整
【发布时间】:2013-07-16 14:39:09
【问题描述】:

我有一个 LinearLayout,它占据了整个屏幕的底部,就像 Mac OSX 停靠栏或 Windows 的任务栏一样。我在没有 xml 的情况下以编程方式创建它,因为内容将是动态的。但是,我对图标(即 ImageView 对象)的定位有疑问。我想指定图标之间的距离(我希望一些图标彼此更近,一些更远..也可以选择在开始或结束时保留一些空间),但是 setMargins、setGravity 等都是失败,没有明显的视觉变化。

这是它目前的样子:

这适用于线性布局:

setOrientation( LinearLayout.HORIZONTAL );

这适用于 ImageView(LinearLayout 中的图标):

LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, (1) ); // weight has to be specified, otherwise only will show 1 icon
imageIcon.setGravity( Gravity.CENTER_HORIZONTAL );

如果没有指定重量:

这适用于线性布局:

setOrientation( LinearLayout.HORIZONTAL );

这适用于 ImageView(LinearLayout 中的图标):

LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT /* , (1)  */); // weight has to be specified, otherwise only will show 1 icon
imageIcon.setGravity( Gravity.CENTER_HORIZONTAL );

问题:

  1. 如何控制ImageView里面的距离 线性布局?你能解释一下用于的代码吗 控制它?我一直在徒劳地搜索Android文档...

  2. 谁能解释 LayoutParams 中“重量”参数的神奇之处?我花了很长时间调试丢失的 ImageView 并通过反复试验找到了神奇的“重量”参数, 但仍然无法弄清楚为什么它会做如此强大的事情 文档中有明确的解释

编辑:为 LinearLayout 插入代码(绿色容器)

public class GameSliderView extends LinearLayout {

    private Context                 mContext;
    private Vector<GameEntryView>   mGameEntries;

    public GameSliderView(Context context) {
        super(context);
        mContext = context;
        mGameEntries = new Vector<GameEntryView>();
        setOrientation( LinearLayout.HORIZONTAL );
    }

    public void addGameEntry( String szIconUrl ) {
        GameEntryView gameEntry = new GameEntryView(mContext);
        LayoutParams lp = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );
        this.setGravity( Gravity.CENTER_HORIZONTAL );
//      lp.setMargins( 0, 0, 0, 0 );
        gameEntry.setLayoutParams( lp );
        gameEntry.loadIcon( szIconUrl );
        gameEntry.requestLayout();
        mGameEntries.add( gameEntry );
        addView( gameEntry );
    }

}

游戏图标:

public class GameEntryView extends RelativeLayout {

    private Context         mContext;
    private GameIconView    mGameIcon;
//  private ImageView       mNewIcon;

    private String          mIconUrl;

    public GameEntryView(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onLayout( boolean changed, int l, int t, int r, int b ) {
        int iChildCount = this.getChildCount();
        for ( int i = 0; i < iChildCount; i++ ) {
            View pChild = this.getChildAt(i);
            pChild.layout(0, 0, pChild.getMeasuredWidth(), pChild.getMeasuredHeight());
        }
    }

    @Override
    protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
        int iParentWidth = MeasureSpec.getSize( widthMeasureSpec );
        int iParentHeight = MeasureSpec.getSize( heightMeasureSpec );
        this.setMeasuredDimension( iParentWidth, iParentHeight );

        int iChildCount = this.getChildCount();
        for ( int i = 0; i < iChildCount; i++ ) {
            View pChild = this.getChildAt(i);
            this.measureChild( 
                    pChild,
                    MeasureSpec.makeMeasureSpec( iParentWidth, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec( iParentHeight, MeasureSpec.EXACTLY)
            );
        }
    }

    public void loadIcon( String szUrl ) {
        mGameIcon = new GameIconView( mContext );
        addView( mGameIcon );

        ImageManager.getInstance( mContext ).get( szUrl, new OnImageReceivedListener() {

            @Override
            public void onImageReceived(String source, Bitmap bitmap) {
                mGameIcon.setImageBitmap( bitmap );
                mGameIcon.setLayoutParams( new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
                postInvalidate();
            }
        });
    }

    public String getIconUrl() {
        return mIconUrl;
    }

    /**
     *  @author Hakim Hauston
     *  @desc   Resizable ImageView - surprised that Android library did not include this?
     *  @ref    http://stackoverflow.com/questions/5554682/android-imageview-adjusting-parents-height-and-fitting-width
     */
    class GameIconView extends ImageView {

        public GameIconView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
            Drawable d = getDrawable();
            if ( d != null ) {
                float fScale = 0.90f;
                int iHeight = (int) (MeasureSpec.getSize( heightMeasureSpec ) * fScale);
                int iWidth = (int) ((iHeight * d.getIntrinsicWidth() / d.getIntrinsicHeight()) * fScale);
                setMeasuredDimension( iWidth,  iHeight );
                Log.d("GameEntryView", "GameEntryView.onMeasure: measureSpec: (" + widthMeasureSpec + ", " + heightMeasureSpec + ");");
                Log.d("GameEntryView", "GameEntryView.onMeasure: intrinsic: (" + d.getIntrinsicWidth() + ", " + d.getIntrinsicHeight() + ");");
                Log.d("GameEntryView", "GameEntryView.onMeasure: calculated: (" + iWidth + ", " + iHeight + ");");
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }

    }

}

【问题讨论】:

  • 显示一些代码如何动态添加视图。然后我会解释你。
  • 为什么不使用相对布局?定位好很多。至于权重,您可以使用 layout_weight 指定多个视图之间的大小比例。例如,如果您希望某些东西使用屏幕的一半或屏幕的 3/4 等。
  • 权重参数用于在宽度/高度总和大于布局的情况下分配空间,因此将 0dp 设置为图像视图的宽度,您将自定义图像之间的比例,假设您有 5 个图像,每个布局权重都为 1,那么您的布局将在宽度方面分为 5 个部分,但是如果您仅增加或减少其中一个的重量,则其宽度会增加或减少,所以我的建议是首先给它们每个 1 和 0dp 宽度,然后根据需要调整权重
  • @Pratik:我插入了整个类,其中可能包含不相关但必要的代码。 setOrientation、setLayoutParams、setMargins 和 setGravity 还不够吗?我可能会错过集*其他强硬
  • @OnurA。如果我需要所有 5 块相互粘连怎么办? (或由指定的 px/dp 分隔)?在 xml 中,我只指定: android:layout_width="wrap_content" 并且添加的下一个视图只是自动捕捉到它...我不能在 Java 中具有相同的下一个视图将重叠它,除非我指定权重..

标签: android layout view android-linearlayout


【解决方案1】:

感谢@Luksprog 和@OnurA。以获得评论中的见解和帮助。我最终将孩子的宽度和高度设置为 WRAP_CONTENT,LinearLayout 设置为水平,并将权重设置为 1。 然后我将子级上的 LayoutParam 的宽度和高度设置为所需的宽度和高度,这样,我设法控制了子级的定位。

谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 2013-11-01
    • 2018-08-10
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多