Android 忽略LinearLayout的编程边距

Android 忽略LinearLayout的编程边距,android,layout,imageview,layoutparams,Android,Layout,Imageview,Layoutparams,我有一个Android布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dice_container" android:layout_width="fill_parent" android:layout_height="fill_parent

我有一个Android布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dice_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="top" >

    <FrameLayout
        android:id="@+id/die_frame_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|top" >

        <ImageView
            android:id="@+id/die1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/dice"
            android:src="@drawable/d6" />

        <ImageView 
            android:id="@+id/die_overlay_1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:alpha="0.4"
            android:visibility="invisible"
            android:src="@drawable/gray_shape" 
            android:contentDescription="@string/gray_overlay" />

    </FrameLayout>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <FrameLayout
            android:id="@+id/die_frame_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/die2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:contentDescription="@string/dice"
                android:src="@drawable/d2" />

            <ImageView 
                android:id="@+id/die_overlay_2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:alpha="0.4"
                android:visibility="invisible"
                android:src="@drawable/gray_shape" 
                android:contentDescription="@string/gray_overlay" />

        </FrameLayout>

        <FrameLayout
            android:id="@+id/die_frame_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/die3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:contentDescription="@string/dice"
                android:src="@drawable/d1" />

            <ImageView 
                android:id="@+id/die_overlay_3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:alpha="0.4"
                android:visibility="invisible"
                android:src="@drawable/gray_shape"
                android:contentDescription="@string/gray_overlay" />

        </FrameLayout>

    </LinearLayout>

</LinearLayout>

我正试图通过编程来处理图像的位置。问题是结果并不是我想要的,这将是以金字塔为中心的三个图像。模具编号1为顶部,2和3分别为底部左侧和右侧

FrameLayout[] frames = new FrameLayout[] {(FrameLayout) findViewById(R.id.die_frame_1),
            (FrameLayout) findViewById(R.id.die_frame_2),
            (FrameLayout) findViewById(R.id.die_frame_3)};
Point size = getSize();
int width = size.x;
int height = size.y;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) frames[0].getLayoutParams();
// Width will be restricted either by height or width. Dice should take max 50% of screen
int frameWidth = (int) Math.min(width * 0.44, height * 0.5 * 0.44); // Each dice 40%
params.width = frameWidth;
params.height = frameWidth; // Square, so width equals height
// Ensure that the space between each dice is equal
int frameHeightMargin = (int) (height * 0.5 - frameWidth * 2) / 3; // Divide the remainder evenly
int frameWidthMarginOuter = (int) (width - 2 * frameWidth - frameHeightMargin) / 2; // Calculate outer
int frameWidthMarginCenter = (int) frameHeightMargin / 2; // Same spacing between dice
int frameWidthMarginUpper = (int) (width - frameWidth) / 2; // Upper only
for (int i = 0; i < frames.length; i++) {
    if (i == 0) {
        params.setMargins(frameWidthMarginUpper, frameHeightMargin, frameWidthMarginUpper, frameWidthMarginCenter); // Gravity is centered
    } else if (i == 1) { // Left dice
        params.setMargins(frameWidthMarginOuter, frameWidthMarginCenter, frameWidthMarginCenter, frameHeightMargin);
    } else if (i == 2) {// Right dice
        params.setMargins(frameWidthMarginCenter, frameWidthMarginCenter, frameWidthMarginOuter, frameHeightMargin);
    }
    frames[i].setLayoutParams(params);
}
FrameLayout[]frames=newframelayout[]{(FrameLayout)findViewById(R.id.die_frame_1),
(框架布局)findViewById(R.id.die_框架2),
(FrameLayout)findviewbyd(R.id.die\u frame\u 3)};
点大小=getSize();
int width=size.x;
整数高度=大小y;
LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)帧[0]。getLayoutParams();
//宽度将受到高度或宽度的限制。骰子最多占屏幕的50%
int frameWidth=(int)Math.min(宽度*0.44,高度*0.5*0.44);//每个骰子40%
params.width=帧宽度;
params.height=frameWidth;//正方形,所以宽度等于高度
//确保每个骰子之间的间距相等
整数帧高边距=(整数)(高度*0.5-帧宽*2)/3;//平分余数
int frameWidthMarginOuter=(int)(宽度-2*frameWidth-frameHeightMargin)/2;//计算外部
int frameWidthMarginCenter=(int)frameHeightMargin/2;//骰子之间的间距相同
int frameWidthMarginUpper=(int)(宽度-frameWidth)/2;//仅上
对于(int i=0;i
当前结果:


顺便说一下,代码在onCreate方法中。我尝试过使用和不使用
requestLayout()
作为FrameLayouts和父视图。

除非您确切知道自己在做什么,否则不要对多个视图组使用相同的LayoutParams。解决方案非常简单。为每个模具创建一个新的LayoutParams

FrameLayout[] frames = new FrameLayout[] {(FrameLayout) findViewById(R.id.die_frame_1),
        (FrameLayout) findViewById(R.id.die_frame_2),
        (FrameLayout) findViewById(R.id.die_frame_3)};
Point size = getSize();
int width = size.x;
int height = size.y;
// Width will be restricted either by height or width. Dice should take max 50% of screen
int frameWidth = (int) Math.min(width * 0.44, height * 0.5 * 0.44); // Each dice 40%
// Ensure that the space between each dice is equal
int frameHeightMargin = (int) (height * 0.5 - frameWidth * 2) / 3; // Divide the remainder evenly
int frameWidthMarginOuter = (int) (width - 2 * frameWidth - frameHeightMargin) / 2; // Calculate outer
int frameWidthMarginCenter = (int) frameHeightMargin / 2; // Same spacing between dice
int frameWidthMarginUpper = (int) (width - frameWidth) / 2; // Upper only
for (int i = 0; i < frames.length; i++) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.width = frameWidth;
    params.height = frameWidth; // Square, so width equals height
    if (i == 0) {
        params.setMargins(frameWidthMarginUpper, frameHeightMargin, frameWidthMarginUpper, frameWidthMarginCenter); // Gravity is centered
    } else if (i == 1) { // Left dice
        params.setMargins(frameWidthMarginOuter, frameWidthMarginCenter, frameWidthMarginCenter, frameHeightMargin);
    } else if (i == 2) {// Right dice
        params.setMargins(frameWidthMarginCenter, frameWidthMarginCenter, frameWidthMarginOuter, frameHeightMargin);
    }
    frames[i].setLayoutParams(params);
}
FrameLayout[]frames=newframelayout[]{(FrameLayout)findViewById(R.id.die_frame_1),
(框架布局)findViewById(R.id.die_框架2),
(FrameLayout)findviewbyd(R.id.die\u frame\u 3)};
点大小=getSize();
int width=size.x;
整数高度=大小y;
//宽度将受到高度或宽度的限制。骰子最多占屏幕的50%
int frameWidth=(int)Math.min(宽度*0.44,高度*0.5*0.44);//每个骰子40%
//确保每个骰子之间的间距相等
整数帧高边距=(整数)(高度*0.5-帧宽*2)/3;//平分余数
int frameWidthMarginOuter=(int)(宽度-2*frameWidth-frameHeightMargin)/2;//计算外部
int frameWidthMarginCenter=(int)frameHeightMargin/2;//骰子之间的间距相同
int frameWidthMarginUpper=(int)(宽度-frameWidth)/2;//仅上
对于(int i=0;i
您是否需要以编程方式执行此操作?在xml中使用布局不是更简单吗?这是一个公平的问题。我想用这种方式来解决所有设备的问题。虽然我开始觉得我会比手工方式更好,我明白了。我现在帮不了你,祝你好运。