开发者

How to align title of Activity on center or anywhere in android?

((TextView)((FrameLayout)((LinearLayout)((ViewGroup)getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);

I have used this above code for aligning my title of activi开发者_如何转开发ty in center but it will gave me a error i.e. "FrameLayout cannot be resolved to a type" and if removed the FrameLayout then it will give me the same error in LinearLayout. I have also change the title bar through android.xml file by adding these line:

          <application  android:label="@string/app_name">
          <activity android:name=".OptionsmenuAct"
              android:label="@string/app_name"

              android:theme="@android:style/Theme.Translucent">

         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>

Now I am trying to align according to my need and also want to increase size of title text.

How can I do this plz help me............


You'll actually want to use a custom title view for this. Check out this question : how to set custom title bar TextView Value dynamically in android?


((TextView)((LinearLayout)((ViewGroup) getWindow()
   .getDecorView()).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CEN‌​TER); 

This worked for me. But this is an undocumented way of doing it. Google has been known to radically change undocumented data structures, and there's no guarantee that this particular technique will work in the future. So I will recommend u to use it in a try/catch block, like:

try {   
((TextView)((LinearLayout)((ViewGroup) getWindow()
   .getDecorView()).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
        } catch (Exception e1) {
            e1.printStackTrace();
        }


I make a bit clearly variant of usman method:

((TextView)getWindow().getDecorView().findViewById(android.R.id.title)).setGravity(Gravity.CENTER);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜