如何根据设备的宽度设置edittext的宽度

问题描述 投票:0回答:1

我正在尝试根据设备的大小设置一个edittext的大小-200像素(用于两个按钮)

已尝试:

Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x - 200;

        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);

        editTextmessage = findViewById(R.id.input_chat_message);

        editTextmessage.setLayoutParams(lparams);

        editTextmessage.getLayoutParams().width = width;

和:editTextmessage.setWidth(size.x - 200);有什么解决办法吗?ps:在问这个问题之前,我已经用Google搜索甚至搜索了stackoverflow编辑:activity_chat.xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChatActivity">

    <include
        android:id="@+id/chat_toolbar"
        layout="@layout/app_bar_layout"

        />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/chat_toolbar"
        android:id="@+id/chat_list"
        android:layout_above="@id/private_chat_linear_layout"/>

    <LinearLayout
        android:id="@+id/private_chat_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <EditText
            android:id="@+id/input_chat_message"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:hint="@string/write_your_message_here"
            android:padding="0dp"
            android:background="@drawable/inputs"/>

        <ImageButton
            android:id="@+id/send_message_button_chat"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/send_message" />
        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/send_file_button_chat"
            app:srcCompat="@drawable/share_file"
            android:background="#D6D7D7"
            />

    </LinearLayout>




</RelativeLayout>

如果需要,请提供更多代码

android xml android-edittext width
1个回答
0
投票

我不确定这是否是您要的内容,但我尝试过这种方式。

    EditText editText = findViewById(R.id.input_chat_message);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
   // int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

   int editTextWidth = width - 200;
   editText.setLayoutParams(new LinearLayout.LayoutParams(editTextWidth,LinearLayout.LayoutParams.WRAP_CONTENT));

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.