Android LinearLayout边距-无法调整

Android LinearLayout边距-无法调整,android,layout,view,android-linearlayout,Android,Layout,View,Android Linearlayout,我有一个线性布局,它占据了整个屏幕的下半部分,就像Mac OSX dock或Windows的任务栏一样。由于内容将是动态的,因此我在不使用xml的情况下以编程方式创建它。但是,我在图标(即ImageView对象)的定位方面遇到了问题。我想指定图标之间的距离(我希望一些图标彼此更靠近,而另一些更远离..也可以选择在开始或结束时保留一些空间),但是setMargins、setGravity等都失败了,没有明显可见的视觉变化 这就是它目前的样子: 这适用于线性布局: setOrientation(

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

这就是它目前的样子:

这适用于线性布局:

setOrientation( LinearLayout.HORIZONTAL );
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 );
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 );
这将应用于图像视图(LinearLayout中的图标):

如果未指定重量:

这适用于线性布局:

setOrientation( LinearLayout.HORIZONTAL );
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 );
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 );
这将应用于图像视图(LinearLayout中的图标):

问题是:

  • 如何控制内部ImageView之间的距离 线性布局?请你解释一下过去的代码好吗 控制它?我一直在Android文档中搜索,但都没有找到

  • 有人能解释一下LayoutParams中的“重量”参数的神奇作用吗?我花了很长时间调试丢失的ImageView,并通过反复试验找到神奇的“权重”参数, 但还是不明白为什么它在没有 文件中有明确的解释

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

    公共类GameSliderView扩展了LinearLayout{
    私有上下文;
    专用向量机条目;
    公共游戏幻灯片视图(上下文){
    超级(上下文);
    mContext=上下文;
    mGameEntries=新向量();
    设置方向(线性布局。水平);
    }
    public void addGameEntry(字符串szIconUrl){
    GameEntryView gameEntry=新GameEntryView(mContext);
    LayoutParams lp=新的LayoutParams(LayoutParams.MATCH\u父级,LayoutParams.MATCH\u父级);
    这是设置重力(重心水平);
    //lp.setMargins(0,0,0,0);
    gameEntry.setLayoutParams(lp);
    gameEntry.loadIcon(sziconur);
    gameEntry.requestLayout();
    mGameEntries.add(游戏条目);
    addView(游戏入口);
    }
    }
    
    游戏图标:

    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);
                }
            }
    
        }
    
    }
    
    公共类GamentryView扩展了RelativeLayout{
    私有上下文;
    私人游戏查看mGameIcon;
    //私有ImageView mNewIcon;
    私有字符串mIconUrl;
    公共游戏入口视图(上下文){
    超级(上下文);
    mContext=上下文;
    }
    @凌驾
    仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
    int-iChildCount=this.getChildCount();
    对于(int i=0;i
    感谢@Luksprog和@OnurA。获取评论中的见解和帮助。最后,我将子对象的宽度和高度设置为包裹内容,将LinearLayout设置为水平,并将权重设置为1。 然后我设置布局的宽度和高度pa