我想在我的按钮栏中的按钮之间制作一个自定义分隔符,我把那个按钮栏放在一个HorizontalScrollView
中,我使用了两种不同的方法来做到这一点,但都不起作用,第一个是在每两个按钮之间放置一个View
,就像这样:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="top"
style="@android:style/ButtonBar"
android:background="@drawable/released" >
<Button
android:id="@+id/Main"
android:layout_width="120dp"
android:layout_height="50dp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold"
android:text="@string/main"/>
<View android:layout_height="fill_parent"
android:layout_width="2px"
android:background="@drawable/separators"/>
<Button
android:id="@+id/HomeView"
android:layout_width="120dp"
android:layout_height="50dp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold"
android:layout_toRightOf="@+id/Main"
android:text="@string/homeview" />
/! declaring the rest butttons and views!/
</RelativeLayout>
但是在制作时,我只在所有按钮之前的按钮栏中的开头放置了一个分隔符。
第二个是制作一个divider
文件并使用我的main中的分隔符选项.xml如下所示:
<LinearLayout
android:id="@+id/buttonbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="top"
android:divider="@drawable/divider"
android:dividerPadding="12dip"
android:showDividers="middle"
style="@android:style/ButtonBar"
android:background="@drawable/released" >
/! declaring buttons !/
</LinearLayout>
和分隔线.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="2dip" />
<solid android:color="@drawable/separators" />
</shape>
其中separators
是渐变色,
但是这种方式给了我这个错误:
java.lang.NullPointerException
at com.android.layoutlib.bridge.impl.RenderSessionImpl.getDefaultProperties(RenderSessionImpl.java:1476)
at com.android.layoutlib.bridge.BridgeRenderSession.getDefaultProperties(BridgeRenderSession.java:68)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.ViewHierarchy.getDefaultProperties(ViewHierarchy.java:710)
at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getStringValue(XmlProperty.java:209)
at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getValue(XmlProperty.java:221)
at com.android.ide.eclipse.adt.internal.editors.layout.properties.BooleanXmlPropertyEditor.paint(BooleanXmlPropertyEditor.java:52)
at org.eclipse.wb.internal.core.model.property.table.PropertyTable.drawProperty(PropertyTable.java:1309)
at org.eclipse.wb.internal.core.model.property.table.PropertyTable.drawContent(PropertyTable.java:1151)
at org.eclipse.wb.internal.core.model.property.table.PropertyTable.handlePaint(PropertyTable.java:1094)
at org.eclipse.wb.internal.core.model.property.table.PropertyTable.access$200(PropertyTable.java:64)
at org.eclipse.wb.internal.core.model.property.table.PropertyTable$3.handleEvent(PropertyTable.java:187)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1081)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1066)
at org.eclipse.swt.widgets.Composite.WM_PRINTCLIENT(Composite.java:1667)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4673)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:340)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4977)
at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2443)
at org.eclipse.swt.widgets.Button.callWindowProc(Button.java:343)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4706)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4990)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2549)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:610)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
关于按钮之间的分隔符位置,当您使用RelativeLayout
时,您需要准确提及您希望分隔符放置的位置。将第一个布局中的分隔符View
替换为以下内容将为您提供所需的结果:-
<View android:id="@+id/separator"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/Main"
android:layout_width="2px"
android:background="@drawable/separators"/>
并在"HomeView
"按钮中进行以下更改
<Button
android:id="@+id/HomeView"
android:layout_width="120dp"
android:layout_height="50dp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold"
android:layout_toRightOf="@+id/Separator"
android:text="@string/homeview" />