我是Android新手,在Android Studio中設計佈局時遇到問題。我發現了很多相關的問題,但都沒有成功。根據當前預覽尺寸(Pixel,Nexus 7 ...),約束值始終由Android Studio在dp中進行硬編碼。ConstraintLayout由Android Studio自動調整大小
代碼我寫在XML編輯器(即工作得很好我的手機上):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<com.meanstreet.note.Slider
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.meanstreet.note.MainActivity">
<RelativeLayout
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bold"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="@string/bold_button" />
...
代碼改變後,我去了設計標籤(不適合我的手機屏幕):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<com.meanstreet.note.Slider
android:id="@+id/slider"
android:layout_width="344dp"
android:layout_height="495dp"
tools:context="com.meanstreet.note.MainActivity"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<RelativeLayout
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bold"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="@string/bold_button" />
...
Slider是擴展了RelativeLayout的自定義視圖。
Slider中的硬編碼值取決於選定的手機以用於Android Studio中的預覽,並且該視圖現在不在我的手機中(如果選擇的手機較大)或右側和底部的邊距較大(如果選擇手機更小)。
這對我來說似乎很奇怪:我仍然可以避免進入設計選項卡,這樣代碼將不會更改,並且可以在任何設備上工作,但這很煩人。
爲什麼Android Studio的行爲如此?如何讓寬度和高度保持在「match_parent」,並且在任何手機上都有合適的尺寸?
在此先感謝您的幫助。
「我發現了很多相關的問題,但都沒有成功。」你發現了哪些相關問題?當您嘗試建議的解決方案時,結果如何?你想要什麼不同於這些結果? –
「我怎樣才能讓寬度和高度停留在」match_parent「?」只需將寬度和高度設置爲「match_parent」即可。 –
https://stackoverflow.com/questions/37505641/android-constraintlayout-generates-absolute-values =>表示值不在代碼中編譯,但似乎是。 https://stackoverflow.com/questions/40126678/android-studio-converting-match-parent-to-0dp =>與0dp,滑塊不顯示。 https://stackoverflow.com/questions/43452384/android-studio-keeps-replacing-match-parent-with-fixed-dp-value =>我試圖推斷常量,但結果更糟糕,邊緣的一半大小屏幕被插入 –