如何在android中使用RelativeLayout进行设计,而不使用所有屏幕密度的静态余量
这是我的如何在android中使用RelativeLayout进行设计,而不使用所有屏幕密度的静态余量,android,android-layout,alignment,android-relativelayout,Android,Android Layout,Alignment,Android Relativelayout,这是我的布局代码(xml) 像 //在某个常量文件中将其定义为最终变量 Display mDisplay= activity.getWindowManager().getDefaultDisplay(); int screenWidth= mDisplay.getWidth(); int screenHeight= mDisplay.getHeight(); 现在你可以做了 PADDING_SIZE_SMALL = screenHeight * (0.1) // 10% percent of
布局
代码(xml)
像
//在某个常量文件中将其定义为最终变量
Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int screenWidth= mDisplay.getWidth();
int screenHeight= mDisplay.getHeight();
现在你可以做了
PADDING_SIZE_SMALL = screenHeight * (0.1) // 10% percent of screen height
PADDING_SIZE_LARGE = screenHeight * (0.25) // 25% percent of screen height
PADDING_SIZE_MEDIUM = screenHeight * (0.15) // 15% percent of screen height
MARGIN_SIZE_SMALL = screenHeight * (0.1) // 10% percent of screen height
MARGIN_SIZE_LARGE = screenHeight * (0.25) // 25% percent of screen height
MARGIN_SIZE_MEDIUM = screenHeight * (0.15) // 15% percent of screen height
HEADER_TEXT_SMALL = screenHeight * (0.25) 25% percent of screen height
HEADER_TEXT_LARGE = screenHeight * (0.35) 35% percent of screen height
LEFT_BAR_WIDTH = screenWidth * (0.45) 45% percent of screen width
现在您可以将所有这些值属性实用地设置为任何元素ListView。文本视图或布局也可以
这是支持Android UI设计的逻辑方式:支持多屏幕。试试这个
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/common_bg"
android:orientation="vertical" >
<Button
android:id="@+id/pendingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/scheduledBtn"
android:background="@drawable/pending_select_bg"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="5dp"
android:text="@string/pending_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans" />
<Button
android:id="@+id/scheduledBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="10dp"
android:background="@drawable/scheduled_deselect_bg"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="5dp"
android:text="@string/scheduled_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans" />
<Button
android:id="@+id/completedBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/scheduledBtn"
android:background="@drawable/completed_deselect_bg"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="5dp"
android:text="@string/completed_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans" />
<Button
android:id="@+id/notificationBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/scheduledBtn"
android:background="@drawable/notifications_icon"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp" />
<ListView
android:id="@+id/commonFragmentListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/pendingBtn"
android:layout_marginBottom="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="12dp"
android:background="@drawable/content_bg"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/divider_appointment"
android:listSelector="@android:color/transparent"
android:padding="10dp" >
</ListView>
</RelativeLayout>
试试这种方法,希望这能帮助您解决问题。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/common_bg"
android:paddingTop="12dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/notificationPendingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notifications_icon"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:textSize="16sp" />
<Button
android:id="@+id/pendingBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/pending_select_bg"
android:text="@string/pending_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans"
android:layout_marginTop="10dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/notificationScheduledBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notifications_icon"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:visibility="gone"/>
<Button
android:id="@+id/scheduledBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/scheduled_deselect_bg"
android:text="@string/scheduled_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans"
android:layout_marginTop="10dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/notificationCompletedBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notifications_icon"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:visibility="gone"/>
<Button
android:id="@+id/completedBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/completed_deselect_bg"
android:text="@string/completed_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans"
android:layout_marginTop="10dp"/>
</FrameLayout>
</LinearLayout>
<ListView
android:id="@+id/commonFragmentListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="12dp"
android:background="@drawable/content_bg"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/divider_appointment"
android:listSelector="@android:color/transparent"
android:padding="10dp" >
</ListView>
</LinearLayout>
为不同的设备创建多个维度。
PADDING_SIZE_SMALL = screenHeight * (0.1) // 10% percent of screen height
PADDING_SIZE_LARGE = screenHeight * (0.25) // 25% percent of screen height
PADDING_SIZE_MEDIUM = screenHeight * (0.15) // 15% percent of screen height
MARGIN_SIZE_SMALL = screenHeight * (0.1) // 10% percent of screen height
MARGIN_SIZE_LARGE = screenHeight * (0.25) // 25% percent of screen height
MARGIN_SIZE_MEDIUM = screenHeight * (0.15) // 15% percent of screen height
HEADER_TEXT_SMALL = screenHeight * (0.25) 25% percent of screen height
HEADER_TEXT_LARGE = screenHeight * (0.35) 35% percent of screen height
LEFT_BAR_WIDTH = screenWidth * (0.45) 45% percent of screen width
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/common_bg"
android:orientation="vertical" >
<Button
android:id="@+id/pendingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/scheduledBtn"
android:background="@drawable/pending_select_bg"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="5dp"
android:text="@string/pending_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans" />
<Button
android:id="@+id/scheduledBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="10dp"
android:background="@drawable/scheduled_deselect_bg"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="5dp"
android:text="@string/scheduled_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans" />
<Button
android:id="@+id/completedBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/scheduledBtn"
android:background="@drawable/completed_deselect_bg"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="5dp"
android:text="@string/completed_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans" />
<Button
android:id="@+id/notificationBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/scheduledBtn"
android:background="@drawable/notifications_icon"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp" />
<ListView
android:id="@+id/commonFragmentListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/pendingBtn"
android:layout_marginBottom="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="12dp"
android:background="@drawable/content_bg"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/divider_appointment"
android:listSelector="@android:color/transparent"
android:padding="10dp" >
</ListView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/common_bg"
android:paddingTop="12dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/notificationPendingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notifications_icon"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:textSize="16sp" />
<Button
android:id="@+id/pendingBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/pending_select_bg"
android:text="@string/pending_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans"
android:layout_marginTop="10dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/notificationScheduledBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notifications_icon"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:visibility="gone"/>
<Button
android:id="@+id/scheduledBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/scheduled_deselect_bg"
android:text="@string/scheduled_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans"
android:layout_marginTop="10dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/notificationCompletedBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notifications_icon"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:visibility="gone"/>
<Button
android:id="@+id/completedBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/completed_deselect_bg"
android:text="@string/completed_txt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="16sp"
android:typeface="sans"
android:layout_marginTop="10dp"/>
</FrameLayout>
</LinearLayout>
<ListView
android:id="@+id/commonFragmentListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="12dp"
android:background="@drawable/content_bg"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/divider_appointment"
android:listSelector="@android:color/transparent"
android:padding="10dp" >
</ListView>
</LinearLayout>