2014-02-22 39 views
0

所以我今天已經嘗試用製作的Android應用程序,但我已經試過LineairLayout做出開山鼻祖屏幕爲我的應用程序,但我無法得到它的權利..Android的RelativeLayout?

所以,我想RelativeLayout的,我看到了我能將我的ImageViews和按鈕移到任何地方。所以我的問題是如果我將這些項目移動到像中心,左下和右下的地方。這是否會成爲問題或所有手機,因爲並非所有手機都具有相同的尺寸?

public class WelcomeActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome_screen_relative); 

     final ImageView logo = (ImageView) findViewById(R.id.myImageView); 

     DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); 
     int displayHeight = metrics.heightPixels; 
     int displayWidth = metrics.widthPixels; 
     float scaledDensity = metrics.scaledDensity; 

     BitmapFactory.Options dimensions = new BitmapFactory.Options(); 
     dimensions.inJustDecodeBounds = true; 
     Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.log, dimensions); 
     int imageHeight = dimensions.outHeight; 
     int imageWidth = dimensions.outWidth; 

     float percentageToMoveViewDown = (float) 20.0; 
     float viewY_float = (float) ((displayHeight/100.0) * percentageToMoveViewDown); 
     int viewY_int = Math.round(viewY_float); 

     RelativeLayout.LayoutParams view_Layout_params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     view_Layout_params.topMargin = viewY_int; 
     logo.setLayoutParams(view_Layout_params); 
     logo.getLayoutParams().height = imageHeight; 
     logo.getLayoutParams().width = imageWidth; 

    } 
+1

不,手機會將它們移動到正確的位置。唯一可能的問題是它們可能會重疊的非常小的手機,具體取決於視圖大小。 –

+0

@GabeSechan - 我會如何防止?任何可以幫助我甚至是文章的代碼? – user2527967

+0

這是你應該提供不同的可繪製文件夾相關的不同分辨率包含圖像是不同的大小。 –

回答

0

那要看情況。如果你給物體固定的尺寸,當然會。對於dp/dpi請務必在Emu或真實設備中進行測試。您還可以創建密度和方向特定佈局以支持多個屏幕。考慮到不僅尺寸有變化,而且還有方面比例和解決方案以及DPI。

對於大多數應用程序RelativeLayout 可能是正確的方法。

你可以在這裏閱讀文章外觀極好一下:http://developer.android.com/guide/practices/screens_support.html

0

如果項目已固定的尺寸,你總是會與一些手機的麻煩。對於大個子來說,它可能太小了,對於小個子來說太小了......
根據我的經驗,Android的小型/普通/大型屏幕對於配置沒有多大幫助,因爲差異太大。
如果您想確保所有內容都位於其所屬的位置,則可以獲取設備指標。這樣,你甚至不需要依賴中心,但你可以用百分比來處理所有你想要的東西。另外,你可以設置百分比,這是偉大的。就像你可以說我想要一個寬度爲屏幕50%的按鈕,不管屏幕有多大。其更多的工作(甚至可能是矯枉過正),但我真的很喜歡這種方法。一旦你發現它在你的課程開始時基本上只是一個複製粘貼。
例子:

DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); 
int displayHeight = metrics.heightPixels; 
int displayWidth = metrics.widthPixels; 
float scaledDensity = metrics.scaledDensity; 

//move some View 20% down: 
float percentageToMoveViewDown = (float) 20.0; 
float viewY_float = (float) ((displayHeight/100.0) * percentageToMoveViewDown); 
int viewY_int = Math.round(viewY_float); 

RelativeLayout.LayoutParams view_Layout_params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
view_Layout_params.topMargin = viewY_int; 
view.setLayoutParams(view_Layout_params); 

//even works with text size for a TextView: 
float percentageToResizeTextViewTextSize = (float) 3.1; 
float textViewTextSize_float = (float) ((displayHeight/100.0) * percentageToResizeTextViewTextSize); 
int textViewTextSize_int = Math.round(textViewTextSize_float/scaledDensity); 
textView.setTextSize(textViewTextSize_int); 

只是一個側面說明了矯枉過正的事情:這應該是唯一的,如果你想支持小型設備(它們大多運行類似的Android 2.3,但仍作爲預算電話銷售)必要大型設備也是如此,但大型設備的麻煩並不像小型設備那樣大。我個人寧願花更多的努力,而不是更少,你永遠不知道。

編輯:ImageView的代碼
最簡單的方法是使用xml和代碼混合使用它。請注意,如果您在xml中將其設置爲0,則必須更改寬度和高度,如下例所示。
只需將它放置在你想要的地方,在你的RelativeLayout的某處。

在你的XML:

<ImageView 
android:id="@+id/myImageView" 
android:layout_width="0dp" 
android:layout_height="0dp" /> 

在你的代碼:

ImageView myImageView = (ImageView)findViewById(R.id.myImageView); 

現在,您可以用myImageView工作,我與視圖和TextView的做到了。你甚至可以在代碼中設置圖像。 此大小爲0,0的imageView現在放置在之前的位置。現在,您可以將寬度設置爲屏幕寬度的50%,並將高度設置爲...讓屏幕高度爲40%。然後你需要放置它。如果你想要居中,你就知道每邊必須有25%的屏幕,所以你可以添加25%左右的邊距。

編輯2:保持原有IMAGESIZE
如果你想保持圖像的原始大小的繪圖資源,你可以得到它的寬度和高度是這樣的:

​​

現在,你有,你可以用它來計算縱橫比來保持它。 現在您知道設備的寬度和高度,您可以輕鬆計算出該圖片需要多少屏幕(imageheight/screenheight * 100 =您希望圖片瀏覽高度的屏幕百分比)。因此,您設置爲imageview的高度將爲displayHeight/100 * (imageHeight/displayHeight * 100)
如何擺放? 現在,如果您將Screenheight設置爲100,將圖像高度設置爲80,則可以獲得80%。你現在將這個百分比從100中分出來。除以2,並且你知道如果你想把它放在屏幕的中間,你會有多少空間作爲頂部和底部邊距(你必須做寬度相同)。
注意:如果您不希望它與屏幕大小相關,但是與原始大小相關,則該百分比方法是毫無意義的。如果您剛纔描述的形象符合您的想象,那麼對於小設備來說它可能仍然太大,對於大設備來說太小。相反,您可以考慮屏幕上其餘部分的百分比與其餘部分的比例,並將其調整到相應的大小,因爲它會在所有設備上具有相對大小。

編輯3:
由於您以原始大小加載圖像,如果它是小圖像,它在大設備上會很小。

//first you need to know your aspect ratio. 
float ratio = imageWidth/imageHeight; 

//lets say you wanted the image to be 50% of the screen: 
float percentageToResizeImageTo = (float) 50.0; 
float imageX_float = (float) ((displayHeight/100.0) * percentageToResizeImageTo); 
int imageX_int = Math.round(imageX_float); 
//now you know how much 50% of the screen is 

imageWidth = imageX_int; 
imageHeight = imageWidth * ratio; 

RelativeLayout.LayoutParams view_Layout_params = new RelativeLayout.LayoutParams(imageHeight, imageWidth); 
view_Layout_params.topMargin = viewY_int; 
logo.setLayoutParams(view_Layout_params); 
logo.setScaleType(ImageView.ScaleType.Fit_XY); 
+0

非常好的主意@tritop。我將如何使用代碼在佈局上放置ImageView? – user2527967

+0

通過編輯向您展示如何添加Imageview。 – tritop

+0

好的,所以我試了一下,它使圖像變大了很多。我將如何保持它相同的大小? (但它仍會將其移至特定位置) – user2527967