如何根据屏幕大小以编程方式调整这些图层中的RelativeLayout / LinearLayout和内容的大小

时间:2017-12-14 18:58:24

标签: android android-layout

我正在开发一个Android应用程序,这是第一次。我来自网络环境,我不确定我是否可以通过Android UI转移我的技能。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:gravity="center_vertical|center_horizontal">

<LinearLayout
    android:id="@+id/SignUpHolder"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_light"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="15dp">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@string/Yeah"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/someTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="@string/sign_up_with_string_sign_up_with_facebook"
        android:textSize="25sp" />
  </LinearLayout>
</RelativeLayout>

如你所见

  

机器人:layout_width =&#34; 350dp&#34;

layout_width设置为350dp硬编码。 这样,这个LinearLayer不会触摸手机屏幕,而且会出现在各种手机屏幕上。

我正在寻找一种方法将此LinearLayer的宽度减少20-50dp,但不要太小,以便切断ImageView和TextView。

有没有办法获取手机屏幕宽度,计算它并通过某些dp动态设置此图层?

2 个答案:

答案 0 :(得分:0)

实现我想你想要的一个简单方法是将match_parent宽度与水平边距结合起来。像这样:

<LinearLayout
    android:id="@+id/SignUpHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="@android:color/holo_blue_light"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="15dp">

这会导致您的LinearLayout与其中的任何父级一样宽......每侧减去20dp

答案 1 :(得分:0)

您可以添加保证金。不要对宽度进行硬编码,而是在线性布局标记中使用它

    android:layout_margin="30dp"
    android:layout_width="wrap_content"