android - 如何根据设备在android中的宽度设置edittext的宽度?
问题描述
我正在尝试根据设备的大小设置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:在问这个问题之前,我已经用谷歌搜索甚至搜索过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>
如果需要,请索取更多代码
编辑:感谢@Deepika 的代码,现在它可以工作了。但在不同的设备上有所不同。例如:在 Redmi 2 上: 在 Redmi7S 上: 在 Oneplus 7 Pro 上: 那么我该如何解决这个问题?
解决方案
我不确定这是否是您正在寻找的,但我尝试过这种方式。
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));
我希望它有所帮助。
编辑:在xml中,可以这样做。我在这里为 LinearLayout 使用 android:weightSum 属性。
<?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">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/chat_list"
android:layout_above="@id/private_chat_linear_layout"/>
<LinearLayout
android:weightSum="1"
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:layout_weight=".74"
android:id="@+id/input_chat_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Write your message"
android:padding="0dp" />
<ImageButton
android:layout_weight=".13"
android:id="@+id/send_message_button_chat"
android:layout_width="0dp"
android:layout_height="50dp"
app:srcCompat="@drawable/send_message"
tools:ignore="VectorDrawableCompat" />
<ImageButton
android:layout_weight=".13"
android:layout_width="0dp"
android:layout_height="50dp"
android:id="@+id/send_file_button_chat"
app:srcCompat="@drawable/share_file"/>
</LinearLayout>
</RelativeLayout>
推荐阅读
- sql - 如何在 PROC SQL 中格式化数据
- geocoding - 计算用户之间的距离是 Geocoding API 的一项功能吗?
- sql - 在 where 子句中使用列值作为变量名 [已解决]
- python - MySQLdb._exceptions.OperationalError OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
- python - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 3: invalid start byte (Python) socket programming
- c - 如何在不使用 chdir() 函数的情况下在我自己的 C 语言 shell 中实现 cd 命令?
- python - 我无法获得执行命令的按钮。(tkinter)
- python - 安装python依赖项时Docker容器构建错误
- javascript - JS输入onClick未定义
- virtual-machine - 虚拟化特定环境(CPU、缓存、时钟)