我有一个LinearLayout
,它的边距被切断了,我试图摆弄XML以及FlyOutContainer
类来修复它,没有运气。
我希望我的LinearLayout
适合任何Android屏幕。谁能指出我如何解决这个问题的正确方向?并且任何人都可以向我解释为什么边际被切断,所以如果再次发生这种情况我知道该怎么办?
以下是我的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>
感谢任何指导。
答案 0 :(得分:0)
所以我通过从我的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.xml
res
中看到了这个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>
删除该行允许我保留背景