Android布局标题栏不可见

时间:2015-03-29 04:57:03

标签: android xml layout titlebar

我创建了xml布局文件。当我把它作为内容提供给我的活动时,标题栏是不可见的。我怎样才能让它可见我尝试使用setTitle(" Title");

protected void onCreate(Bundle savedInstanceState) { 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
super.onCreate(savedInstanceState); 
Bundle b = getIntent().getExtras(); 
int layoutId = b.getInt("layout");
Log.d("====>>>", " " + layoutId);
setContentView(layoutId); 
setTitle("Title"); 
}

在onCreate()方法中起作用。我不工作。这是我的xml文件。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.universityapp.enrollee.FacultyInformationActivity"
    android:scrollbars="vertical"
    >

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/text_faculty_information_agro"
        android:textSize="15dp"
        android:textAlignment="center"
    />
</RelativeLayout>
</ScrollView>

我通过ListView开始活动:

 profListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position){
                case 0:
                    facultyInfoIntent = new Intent(getBaseContext(), FacultyInformationActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("layout", facultyLayoutNames[position]);
                    facultyInfoIntent.putExtras(b);
                    startActivity(facultyInfoIntent);
            }

            Log.d("Selected faculty", facultiNames[position]);
        }
    });

这是我的清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.universityapp.universityapp" >

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.universityapp.common.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.universityapp.common.MenuActivity"
            android:label="Главное меню" >
        </activity>
        <activity
            android:name="com.universityapp.enrollee.ContacsActivity"
            android:label="Наши контакты" >
        </activity>
        <activity
            android:name="com.universityapp.enrollee.Map"
            android:label="@string/title_activity_map" >
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name="com.universityapp.enrollee.MapsActivity"
            android:label="@string/title_activity_maps" >
        </activity>
        <activity
            android:name="com.universityapp.bachelor.PlatonusActivity"
            android:label="@string/title_activity_platonus" >
        </activity>
        <activity
            android:name="com.universityapp.database.DatabaseActivity"
            android:label="@string/title_activity_database" >
        </activity>
        <activity
            android:name="com.universityapp.enrollee.EnrUniversityInfo"
            android:label="@string/title_activity_enr_university_info" >
        </activity>
        <activity
            android:name="com.universityapp.enrollee.ProfessionsActivity"
            android:label="@string/title_activity_professions" >
        </activity>
        <activity
            android:name="com.universityapp.enrollee.FacultyInformationActivity"
            android:label="@string/title_activity_faculty_information" >
        </activity>
    </application>

</manifest>

6 个答案:

答案 0 :(得分:15)

变化:

<time>
  Article posted: <span>2016-09-16</span>
</time>

在您的班级文件中:

public class MainActivity extends Activity{

}

答案 1 :(得分:10)

我找到了答案!我已将Activity更改为ActionBarActivity,它可以正常工作!

答案 2 :(得分:1)

要显示标题栏,您可以在onCreate

中使用此标题栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

答案 3 :(得分:1)

你可以这样做(如suggested by HafizWaleedHussain):

public void showTitle() {
            try {
                    ((View) findViewById(android.R.id.title).getParent())
                                    .setVisibility(View.VISIBLE);
            } catch (Exception e) {
            }
            getWindow().addFlags(
                            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

如果这不起作用,请打开Android Manifest文件并删除该行(如果存在):

 android:theme="@android:style/Theme.NoTitleBar"

答案 4 :(得分:1)

获得styles.xml并制作新样式

<style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
        <item name="android:windowNoTitle">false</item>
     </style>

并从Manifest调用它(您还可以在项目标记内添加其他功能)

 <activity
            android:name=".st.activity.MyGoogleMap"
            android:theme="@style/CustomWindowTitleBackground">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

答案 5 :(得分:0)

I removed the statement android:theme="@style/AppTheme" in the application tag and the bar started appearing

   <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        **android:theme="@style/AppTheme"**>