【问题标题】:Navigation Drawer with header in AndroidAndroid中带有标题的导航抽屉
【发布时间】:2014-08-13 12:14:44
【问题描述】:

借助http://www.learn2crack.com/2014/06/android-sliding-navigation-drawer-example.html

我设法让我的导航抽屉正常工作。我还在我的抽屉中添加了一个标题。在我的标题中,我有一个 Imageview,两个 TextView

数据显示正常,但由于某种原因,每部手机的用户界面都不同。图像被剪切或背景图像过大。

这是我测试过的 Galaxy Nexus 和 Nexus 5 的屏幕截图:

Nexus 5

银河系

这是标头 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearTotalLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/backgroundforprofile"
android:baselineAligned="false"
android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/LinearLayoutforImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight=".30"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imageforprofile"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/profilepic" />
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayoutforText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center"
    android:layout_weight=".70"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textforprofile"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="left"
        android:text="Joe David"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textforprofileemail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="left"
        android:text="JoeDavidJoeDavi@joedavidjoedavid.com"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

有人可以帮我解决这个问题吗?

谢谢!

更新

根据@Rohan Pawar 和@westito 的建议,我将布局更改为相对 ImageView 没有被剪切并且文本正常输出,但背景图像在 Galaxy Nexus 和 Nexus 5 中仍然存在差异。请参阅我的截图如下。

银河系

Nexus 5

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearTotalLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/backgroundforprofile"
android:baselineAligned="false"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageforprofile"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:src="@drawable/profilepic" />

<TextView
    android:id="@+id/textforprofile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageforprofile"
    android:gravity="left"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="Dushyant Prabhu"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textforprofileemail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textforprofile"
    android:layout_toRightOf="@+id/imageforprofile"
    android:gravity="left"
    android:paddingLeft="5dp"
    android:text="developer@sybershocknetworks.com"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

</RelativeLayout>

【问题讨论】:

  • 尝试使用相对布局
  • @RohanPawar:我尝试了相对布局,文本格式现在很完美,但每部手机的背景图像不同。我什至改变了相对布局的高度我没有看到任何区别?请检查我更新的问题
  • 你的背景图片的分辨率是多少?
  • @RohanPawar: 1600 * 900。当宽度正常出现时,为什么不提高高度?
  • 尝试 android:layout_height="match_parent" 到主布局

标签: android android-layout android-linearlayout


【解决方案1】:

问题在于您的布局。而不是 LinearLayouts,尝试使用 RelativeLayout。这种布局效率更高。

<RelativeLayout>
    <ImageView id="@+id/image" />
    <TextView  layout_toRightOf="@+id/image" />
</RelativeLayout>

这是一个“伪”样本。您可以将 TextView 放在 ImageView 旁边

【讨论】:

  • 我试过Relative Layout现在文本格式已经很完美了,但是每部手机的背景图都不一样。我什至改变了相对布局的高度我没有看到任何区别?请检查我更新的问题
【解决方案2】:

@TheDevMan 我有 Nexus 5 和 Galaxy nexus 两种设备,我已经测试过你的代码在这两种设备上都能完美运行

三星 Galaxy Nexus = 720 x 1280 像素,4.65 英寸(~316 ppi 像素密度)

LG Nexus 5 = 1080 x 1920 像素,4.95 英寸(约 445 ppi 像素密度)

但我没有要测试的背景图片

【讨论】:

  • 这是每个个人资料用户的 Google+ 默认封面。
【解决方案3】:

为您的标题尝试此操作并根据需要自定义它,它肯定会起作用

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="12dp"
            android:layout_marginBottom="7dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/image_header"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_marginTop="4dp"
                android:background="@drawable/placeholder"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/nav_user_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="18dp"
                android:text="Krunal Patel"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#ffff"
                android:textSize="19dp" />
        </LinearLayout>

    </LinearLayout>

【讨论】:

  • 我试过你的代码,我不能在 nav_user_name 下面再添加一个文本视图?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多