Android工具栏布局问题
我正在尝试使用工具栏制作底部栏并将其与父级底部对齐。这是它的外观:Android工具栏布局问题
但不知何故,在图像中所提到的,他们是一个很大的空白,由于其布局是不完全居中。为什么会发生?
bottom_tool_bar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimaryLight"
android:elevation="4dp"
android:layout_alignParentBottom="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/origin_select"
android:src="@mipmap/ic_action_play"
android:background="@drawable/feedback_background"
android:onClick="choose_origin_button"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/destination_select_select"
android:src="@mipmap/ic_action_stop"
android:background="@drawable/feedback_background"
android:onClick="choose_destination_button"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/poi_select"
android:src="@mipmap/ic_action_pause"
android:background="@drawable/feedback_background"
android:onClick="choose_interest_button"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
为有关活动的布局文件:
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
>
</include>
<include
android:id="@+id/bottom_tool_bar"
layout="@layout/bottom_tool_bar">
</include>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/plot_path_map"
android:layout_below="@+id/tool_bar"
android:layout_above="@+id/bottom_tool_bar"
android:name="com.google.android.gms.maps.MapFragment"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/submit_button"
android:text="Submit"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:background="@drawable/feedback_background"
android:layout_centerVertical="true"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:visibility="invisible"
android:onClick="submit_button_click"
/>
</RelativeLayout>
如果你想在工具栏放置在底部,这是你应该做的事情。
使用带有alignParentBottom一个的LinearLayout =“真”
<RelativeLayout
android:id="@+id/mainLyt"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Some layout things -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/bottomBar">
<!-- some scrolling content -->
</ScrollView>
<LinearLayout
android:id="@+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<!-- Some Buttons -->
</LinearLayout>
</RelativeLayout>
至于你提到的间距,我相信这是另一个问题,您可以使用您的ToolBar
内的下列属性进行修改。
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
它有点工作。现在左侧的空间已经消失,布局居中。然而,顶部和底部的空间仍然存在。任何解决方案? – Bluesir9
创建一个独立的布局文件并将其命名为tool_bar.xml并确保LinearLayout中是你的根布局。
然后在你的活动类型是:
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
></include>
就给出0.3weight
到所有ImageViews和layout:width
和layout:height
为wrap_content
这已经是一个单独的布局文件,我将其包含在活动布局文件中。您的更改没有任何改进。 – Bluesir9
在您发布的xml中,您没有包含任何布局文件。如果你已经更新了XML,请编辑OP。 –
为中心的意见,你有很多选择的2是以下(我相信第二是最好的):
F首先在你的线性布局中包含重量,我喜欢使用100,因为这有助于我以百分比的方式进行思考,然后包含2个残片framelayout,以便将图像浏览推向水平中心,并通过使用像这样的重量来保持响应特性:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"/>
<ImageView
android:id="@+id/origin_select"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="13"
android:background="@drawable/feedback_background"
android:onClick="choose_origin_button"
android:src="@mipmap/ic_action_play"
/>
<ImageView
android:id="@+id/destination_select_select"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="13"
android:background="@drawable/feedback_background"
android:onClick="choose_destination_button"
android:src="@mipmap/ic_action_stop"/>
<ImageView
android:id="@+id/poi_select"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="13"
android:background="@drawable/feedback_background"
android:onClick="choose_interest_button"
android:src="@mipmap/ic_action_pause"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"/>
</LinearLayout>
正如你可以看到两个FrameLayout里什么都没有,但他们帮助你把你的其他ImageViews的中心,他们既给你的空间,60%的人使用,让你只是一个40%的ImageViews因此它们各有13% 。
在另一方面,你可以使用的RelativeLayout为您的父元素,而不是的LinearLayout和位置ImageView的ID为:destination_select_select
居中对齐于母公司的水平和垂直,然后在一个在中心侧面的另两个imageviews定位中间。 此选项可能是最好的:
就像这样:下面一行
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/origin_select"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/feedback_background"
android:onClick="choose_origin_button"
android:src="@mipmap/ic_action_play"
android:layout_alignLeft="@+id/destination_select_select"
/>
<ImageView
android:id="@+id/destination_select_select"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="match_parent"
android:background="@drawable/feedback_background"
android:onClick="choose_destination_button"
android:src="@mipmap/ic_action_stop"/>
<ImageView
android:id="@+id/poi_select"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignRight="@+id/destination_select_select"
android:background="@drawable/feedback_background"
android:onClick="choose_interest_button"
android:src="@mipmap/ic_action_pause"/>
</RelativeLayout>
使用,以避免在活动课左侧的差距。
mToolbar.setContentInsetsAbsolute(0, 0);
请看下面的代码,以避免顶部和底部空间。
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/origin_select"
android:src="@mipmap/ic_action_play"
android:background="@drawable/feedback_background"
android:onClick="choose_origin_button"
/>
在图像视图width
应该是一个wrap_content
和height
应0dp
为了避免顶部和底部的空间,
添加此行
android:minHeight="0dp"
到<android.support.v7.widget.Toolbar>
属性。
http://*.com/questions/26455027/android-api-21-toolbar-padding –
那么,任何答案都适合你吗? – commonSenseCode