
我为headerView设计了布局,并希望在其右侧添加一个图像图标,就像照片一样。
问题是它会显示像图像中的空间,即使我删除图像布局,空间仍然存在。
我试图更改父布局或子布局的布局宽度,任何参数,如数学父或包装内容或提供一个dp。
我试过官方的演示来修复它,但它没有工作。
有人能教我怎么做吗?


我的nav_heafer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#66CDAA"
android:orientation="horizontal">
<!--the left navigationView-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<!--the left icon-->
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
app:srcCompat="@drawable/ic_grid_on_black_24dp" />
<!--the left textView-->
<TextView
android:id="@+id/logIn"
android:textColor="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pageLogIn"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"/>
<!--the left button-->
<Button
android:id="@+id/logOut"
android:text="@string/logOut"
android:layout_width="65dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<!--I just want a outstanding icon over here,on the right side of navigationView-->
<!--I try add the LinearLayout,but it shows strange-->
</LinearLayout>
[1]: https://i.stack.imgur.com/hQy1d.png
[2]: https://i.stack.imgur.com/E9jyu.png我的MainActivity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context="com.example.user.taiwandigestionsociety_v11.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ToolBarStyle"
app:layout_scrollFlags="scroll|enterAlways"
android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/mainFrame"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.AppBarLayout>
<!--control navigationView width-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>发布于 2016-12-27 10:52:43
好的,最后我理解了您的问题,solved.This应该给您如下所示。

所以我们开始吧!
首先,您想要删除图标后面的白色背景,因此将透明背景设置为导航视图,如下所示。
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
<!-- it works as transparent background color -->
android:background="@android:color/transparent"
app:headerLayout="@layout/navigation_header_support" />对于自定义标题,您应该使用相对布局作为父级,因为您希望关闭图标位于导航面板的最右侧。因此,我们可以对齐这个关闭的图标,根据家长。这一次,将背景颜色设置为您相对父级布局的直接子元素,就像我为线性布局所做的那样。
为了在相对布局中匹配图标,在直线布局中留下一些右边的空白,这样你的图标就会占用剩馀的右边距空间。设置图标的相同宽度,就像你把线性布局从右边推到右边一样。在这里,我从右边留下32 it的边距,并设置我的图标的宽度来匹配它。
为了正确定位该图标,我使用了以下属性。你应该调整一下你的。
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"The nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="64dp"
<!-- background color -->
android:background="@color/blue_2"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:drawableLeft="@drawable/ic_pin_primary"
android:drawablePadding="@dimen/activity_horizontal_margin"
android:drawableStart="@drawable/ic_pin_primary"
android:gravity="center_vertical"
android:typeface="normal"
android:textSize="18sp"
android:text="Location"
android:textStyle="bold" />
<!-- Other items go here -->
</LinearLayout>
<ImageView
android:background="@color/blue_2"
android:layout_alignParentRight="true"
android:layout_marginTop="64dp"
android:layout_alignParentTop="true"
android:src="@drawable/ic_pencil"
android:layout_width="32dp"
android:layout_height="32dp" />
</RelativeLayout>发布于 2016-12-29 09:01:36
好吧,这不是你问题的答案。但是,您可以改进您的布局以及性能。然后根据这一点改变你问题中的导航布局,并再次更新你的问题。然后,它可以更易读和更容易理解您的问题。所以任何人都可以很容易地回答
相反,使用一个方向为水平的线性布局图标和textView,您可以这样做,我如下所示。
你目前的执行情况:
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView9"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_grid_on_black_24dp" />
<TextView
android:id="@+id/newInformation"
android:textSize="15dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/newInformation"
android:textColor="@android:color/background_light"/>
</LinearLayout>现在把它改成这样的东西:
<TextView
android:id="@+id/person_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- this is where your icon goes -->
android:drawableLeft="@drawable/ic_grid_on_black_24dp"
android:drawableStart="@drawable/ic_grid_on_black_24dp"
<!-- your icon's padding -->
android:drawablePadding="@dimen/activity_horizontal_margin"
android:gravity="center_vertical" />请把你的布局改为这样,然后我可以试着回答你的问题。
https://stackoverflow.com/questions/41342229
复制相似问题