【问题标题】:Margins on LinearLayout cut off from top, bottom, left and right. Can't find a fixLinearLayout 上的边距从顶部、底部、左侧和右侧截断。找不到修复
【发布时间】:2014-07-10 20:08:31
【问题描述】:

我有一个 LinearLayout,它的边距被切断,我尝试摆弄 XML 以及 FlyOutContainer 类来修复它,但没有运气。

我希望我的LinearLayout 适合任何安卓屏幕。谁能指出我如何解决这个问题的正确方向?谁能向我解释为什么边距被切断,所以如果再次发生这种情况,我知道该怎么办?

这是我的LinearLayout 菜单的样子:

这是FlyOutContainer 类:

public class FlyOutContainer extends LinearLayout {

private View menu; //Menu of application
private View content; //Content of application

protected static final int menuMargin = 150; 

public enum MenuState {
    CLOSED, OPEN
};

protected int currentContentOffset = 0;
protected MenuState menuCurrentState = MenuState.CLOSED;

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

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

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

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    this.menu = this.getChildAt(0);

    this.content = this.getChildAt(1);
    this.menu.setVisibility(View.GONE);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right,
        int bottom) {
    if (changed)
        this.calculateChildDimensions();

    this.menu.layout(left, top, right - menuMargin, bottom);

    this.content.layout(left + this.currentContentOffset, top, right
            + this.currentContentOffset, bottom);

}
public void toggleMenu() {
    switch (this.menuCurrentState) {
    case CLOSED:
        this.menu.setVisibility(View.VISIBLE);
        this.currentContentOffset = this.getMenuWidth();
        this.content.offsetLeftAndRight(currentContentOffset);          this.menuCurrentState = MenuState.OPEN;
        break;

    case OPEN:
        this.content.offsetLeftAndRight(-currentContentOffset);
        this.currentContentOffset = 0;
        this.menuCurrentState = MenuState.CLOSED;
        this.menu.setVisibility(View.GONE);
        break;
    }
    this.invalidate();
}

private int getMenuWidth() {
    return this.menu.getLayoutParams().width;
}

private void calculateChildDimensions() {
    this.content.getLayoutParams().height = this.getHeight();
    this.content.getLayoutParams().width = this.getWidth();

    this.menu.getLayoutParams().width = this.getWidth() - menuMargin;
    this.menu.getLayoutParams().height = this.getHeight();
}

}

这是我的 XML:

<FlyOutContainer.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
style="@style/Container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_Child"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <com.facebook.widget.ProfilePictureView
    android:id="@+id/profilePicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    facebook:is_cropped="true"
    facebook:preset_size="small" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/button_1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/button_2" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/button_3" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/button_4" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/button_5" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/button_6" />
</LinearLayout>

<!-- The navigation drawer -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/defined_Text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:text="@string/Toggle_Menu" />
</LinearLayout>

不胜感激。

【问题讨论】:

    标签: android android-layout screen android-linearlayout margin


    【解决方案1】:

    所以我通过从我的 XML 中取出 style="@style/Container 解决了这个问题,出于某种奇怪的原因,我的自定义主题是在布局的所有四个边上都添加了额外的填充。

    所以总而言之,将 XML 更改为此修复了它:

    <FlyOutContainer.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    ....
    
    ....
    
    ....
    
    </FlyOutContainer.FlyOutContainer>
    

    改进的答案:

    如果你不能保持你的背景正确,那么 UI 设计的意义何在 :) ?为了保持我之前实现的风格,我看到在我的 styles.xmlres 中有一个额外的 16 dp 填充,如下所示:

    <style name="Container" >
        <item name="android:layout_margin">0dp</item>
        <item name="android:padding">16dp</item> <!-- REMOVE HERE!!! -->
        <item name="android:orientation">vertical</item>
        <item name="android:background">@drawable/background</item>
    </style>
    

    删除那条线让我可以保留我的背景

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 2012-05-02
      相关资源
      最近更新 更多