What I want to achieve: Center layout with both orientation.
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fffffce0">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fffffce0"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="121dp"
android:background="#fffffce0"
android:layout_marginTop="42dp"
android:id="@+id/linearLayout"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/button9"
android:background="@drawable/custom_button_square"
android:text="@string/a_d"
android:textColor="#ffff"
android:textSize="35sp"
android:onClick="goToAd"/>
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/button10"
android:background="@drawable/custom_button_square"
android:text="@string/e_h"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:background="#fffffce0"
android:id="@+id/linearLayout2">
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/button11"
android:background="@drawable/custom_button_square"
android:text="@string/i_l"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/button12"
android:background="@drawable/custom_button_square"
android:text="@string/m_p"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_below="@+id/linearLayout2"
android:layout_centerHorizontal="true"
android:background="#fffffce0"
android:gravity="center_horizontal">
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/button13"
android:background="@drawable/custom_button_square"
android:text="@string/m_p"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/button14"
android:background="@drawable/custom_button_square"
android:text="@string/q_t"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
</RelativeLayout>
PROBLEM: Everything was ok, until the moment i was tested my app on tablet with bigger screen. When orientation is landscape, everyting is ok, but when i rotate screen, then it looks like that: http://zapodaj.net/b2780f7e637ab.png.html
I tried to set my gravity i Llayout to center, but it didnt work.
答案 0 :(得分:1)
because of the different screen resolutions, try not to use those hard-coded digits like android:layout_height="70dp"
for example. You can set the layout parameters programmatically for each element of your layout respectively to the actual screen resolution and in this case it will work on every screen size and oriantation. Try with something like this:
//declare two global variables that will have the width and height values
private int width;
private int height;
//define a method that will set the values to the both variables
private static void getScreenResolution(Context context){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
}
In your onCreate()
method first call the method above to get the values and then set the layout params (just a simple example):
getScreenResolution(this); //if you are in a fragment, type getActivity() instead of this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(width/10, height/15, 0 ,0); //left, top, right, bottom
yourLinearLayout.setLayoutParams(layoutParams);
Hopefully this helps you to create more general designs in the future :)
答案 1 :(得分:0)
Well ! Like This. Try This..
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="121dp"
android:layout_marginTop="42dp"
android:background="#fffffce0"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button9"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/ic_launcher"
android:onClick="goToAd"
android:text="a_d"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:id="@+id/button10"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/ic_launcher"
android:text="e_h"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#fffffce0"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button11"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/ic_launcher"
android:text="i_l"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:id="@+id/button12"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/ic_launcher"
android:text="m_p"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#fffffce0"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button13"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/ic_launcher"
android:text="m_p"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:id="@+id/button14"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/ic_launcher"
android:text="q_t"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
答案 2 :(得分:0)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >
<RelativeLayout android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffffce0"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#fffffce0"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/button9"
android:layout_width="0dip"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:onClick="goToAd"
android:text="a_d"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:id="@+id/button10"
android:layout_width="0dip"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:text="e_h"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#fffffce0"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/button11"
android:layout_width="0dip"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:text="i_l"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:id="@+id/button12"
android:layout_width="0dip"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:text="m_p"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:weightSum="2"
android:background="#fffffce0"
android:orientation="horizontal" >
<Button
android:id="@+id/button13"
android:layout_width="0dip"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:text="m_p"
android:textColor="#ffff"
android:textSize="35sp" />
<Button
android:id="@+id/button14"
android:layout_width="0dip"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:text="q_t"
android:textColor="#ffff"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>