Android对不同屏幕尺寸的支持

时间:2019-01-24 06:18:41

标签: android android-button android-relativelayout screen-size android-layoutparams

我已经创建了这个布局。在其中显示textview及其下方,它具有用于insta,fb,copy和tweet的共享按钮。

content.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
android:orientation="vertical"
    android:id="@+id/Main"
    android:layout_gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">

    <RelativeLayout
        android:id="@+id/Sub_main"
        android:layout_width="match_parent"
        android:gravity="bottom"
        android:background="@drawable/linerlayout_background"
        android:layout_height="match_parent">


        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"


      android:layout_margin="15dp"
        android:id="@+id/txt_hash"/>

        <LinearLayout
            android:id="@+id/sub"
            android:layout_below="@+id/txt_hash"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button">


            <Button android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:background="@drawable/fb"
                android:id="@+id/fb"/>


            <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/insta"
                android:id="@+id/insta"/>


            <Button android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:background="@drawable/tweet"
                android:id="@+id/tweet"/>


            <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/doc"
                android:id="@+id/doc"/>

        </LinearLayout>

</RelativeLayout>

我有recyclerview,其中包含要查看的内容。现在,我想根据每种设备的屏幕尺寸正确安装这些按钮。我对layoutparams一无所知,我已经引用了链接,但是我需要了解如何计算内部特定按钮的大小(Framelayout / RelativeLayout / LinearLayout / Buttons(4))。

2 个答案:

答案 0 :(得分:0)

在您的布局中,它的重量属性可以支持任何设备。 问题是您不了解布局参数。解决该问题。您可以链接到LayoutParams

对于您提到的recyclerView,可以在onCreateViewHolder中获取按钮的布局参数。

 @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.story_item, parent,
            false);
    Button button = view.findViewById(R.id.txtId);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
    //for example:one quater of the device's width
    layoutParams.width="your width";
    layoutParams.height="your height";
    //and so on,the layoutParams also has extra parameter
    button.setLayoutParams(layoutParams);
    ...
    }

答案 1 :(得分:0)

谁想要在不使用constraintview的情况下设置屏幕或创建不同的.xml和图像文件的人,可以在我得到解决方案后遵循此方法。

我已在我的adapter.class中实现

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int deviceWidth = displayMetrics.widthPixels; 
    int deviceHeight= displayMetrics.heightPixels;

    int Linearwidth=    (deviceWidth*692 ) /720;
    int Linearheight =   (deviceHeight *100 ) /1280;

    int iv_width = (deviceWidth *78)/720;
    int iv_height=(deviceHeight*78)/1280;

    int Text_Linear_Width = (deviceWidth * 710)/720;
    int Text_Linear_Height =(deviceHeight * 319)/1280;
    txt_hash.setTextSize(TypedValue.COMPLEX_UNIT_PX, Text_Linear_Width * 28 / 720);

    LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams(iv_width,iv_height);

    lp.leftMargin = (Linearwidth*78)/720;
    lp.rightMargin = (Linearheight*78)/1280;
    lp.bottomMargin=(Linearheight*200)/1280;
    lp.topMargin=(Linearheight*200)/1280;


    fb.setLayoutParams(lp);
    insta.setLayoutParams(lp);
    tweet.setLayoutParams(lp);
    doc.setLayoutParams(lp);