【问题标题】:Title not showing on the mainActivity标题未显示在 mainActivity 上
【发布时间】:2015-08-12 04:07:11
【问题描述】:

我在 Android 操作栏上放置了一个微调器,我已将微调器放在 test.xml 布局中,我在 OnCreate() 方法的主要活动中这样调用它

   ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
    actionBar.setDisplayShowCustomEnabled(true);

    actionBar.setCustomView(R.layout.activity_test);
    setContentView(R.layout.activity_main);
    actionBar.setTitle("Home");

但我的标题没有显示。我想在该操作栏上显示 HOME 作为标题我应该怎么做?

我的活动测试

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/planets_spinner"
        android:layout_width="100dp"
        android:layout_height="50sp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
      android:background="@drawable/arrow2"
        android:layout_weight="0.08"
        android:drawSelectorOnTop="true" />

</RelativeLayout>

【问题讨论】:

  • 你能发布activity_text.xml吗

标签: android android-actionbar android-spinner


【解决方案1】:

我已经通过向我的activity_test添加一个textView解决了这个问题,因为整个活动都显示在ActionBar上,所以我添加了一个文本视图并且问题解决了:)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <Spinner
        android:id="@+id/planets_spinner"
        android:layout_width="100dp"
        android:layout_height="50sp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/arrow2"
        android:drawSelectorOnTop="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/planets_spinner"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="6dp"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:text="Scene" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    当您设置android:layout_alignParentRight="true" 时,RelativeLayout 会使用包括标题空间在内的所有空间,并且不会显示标题。您可以通过在 RelativeLayout 周围绘制边框来检查行为。但是如果您不使用android:layout_alignParentRight="true",RelativeLayout 不会占用更多空间。如果您也使用android:layout_alignParentLeft="true",它也不会占用更多空间。它仅适用于 android:layout_alignParentRight="true" 很奇怪。为了在父级右侧显示视图,以下内容对我有用:

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/profile_image"
            android:layout_width="40dp"
            android:src="@drawable/ic_launcher"
            android:layout_height="40dp"
            android:background="?android:selectableItemBackground"
            android:padding="3dp"
            android:scaleType="centerCrop" /> 
    

    不要使用RelativeLayout。只是您需要的视图。 然后你可以与如下所示的 LayoutParams 对齐:

    actionBar.setCustomView(R.layout.actionbar_layout);
    actionBar.setDisplayShowCustomEnabled(true);
    ImageView imageProfile = (ImageView)actionBar.getCustomView();
    int length = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
    Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
                    length,
                    length, Gravity.RIGHT
                    | Gravity.CENTER_VERTICAL);
    imageProfile.setLayoutParams(layoutParams);
    

    现在您应该可以同时查看 ActionBar 标题和 ImageView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-30
      • 2022-01-24
      • 2016-01-21
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多