2012-12-12 26 views
2

在我的應用程序的頂部,我有一個標題,它應該顯示在中間,一個按鈕在右邊。由於textViews的長度在我的控制之下,我有時候會因爲長度過長而忽略按鈕。使用不同密度的設備自動調整textView android

以下this後,我不知何故傾向於解決這個問題。我的設備是HTC的願望。不幸的是,如果我檢查Galaxy SIII,它並沒有辦法。

我想知道如何以不同密度的不同設備來管理這個問題。

我相對佈局

回答

0

只是在你的XML使用weightsum,使所有的意見,填充母寬度內部控制.....這使得您的TextView

+0

因爲我沒有提到第一個,我使用的是相對佈局,它不佔用weightSum。 – boburShox

+1

ohhhh ....實際上android中最靈活的佈局是linearlayout .....如果你可以改變它,將它轉換爲linearlayout –

0

可以保持佈局的自動調整大小根據自己的DPI`s 複製在XHDPI相同的XML數據(S3落在XHDPI)並對其進行測試同樣的XML數據複製在華電國際 但 牢記佈局以後的事像素比爲 以下

在LDPI其1:0.75 在MDPI其1:1 在HDPI其1:1.5 在XHDPI其1:2

1

還可以檢查由this--

設備屏幕密度
WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 
    int screenWidth = display.getWidth(); 
    int screenHeight = display.getHeight(); 

和最新您的應用程序可以根據需要進行相應的管理..

+0

我需要我的textView的大小正確嗎?但是我不能在onCreate方法中獲得textView的大小,以便像你提到的那樣根據screenHeight工作。它返回0. – boburShox

+0

使用textView.getMeasuredWidth(); ..它會給你文本視圖的寬度和accrodingly計算。 – Hulk

+0

我應該在_onCreate()_方法中分別設置一個text和textSize,如果你使用_getMeasuredWidth_它返回0,我相信 – boburShox

0
Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
DisplayMetrics dm = new DisplayMetrics(); 
display.getMetrics(dm); 
float density = dm.density; 
int screenWidth = display.getWidth(); 

通過上面這段代碼,你有你的屏幕密度爲float ..所以你可以用它來計算你的textView的寬度,如:

int newWidth = (int) (density * 100); 

其中100這裏是基於大小。 或者你可以根據你的screenWdith有一個比例。

int newWidth = screenWidth/2; 
+0

我在想像_setTextSize()_而不是_setWidth()_ – boburShox

+1

好吧,那麼你希望你的文字完全看到......而你不需要將它放置在任何特定的地方? – yahya

+0

是的。只是中間很好。 – boburShox

相關問題