我想實現以下目標:如何創建一個Button至少和TextView一樣長的佈局?
- 我需要的地方放置一個
Button
視圖佈局 - 應該有
TextView
權這Button
就是上面使用的標籤 - 的大小
Button
應取決於TextView
的大小。由於該應用是本地化的,因此文本寬度並不固定,而是取決於用作標籤的本地化文本。 Button
應該有一個分鐘。寬度爲150dp。如果TextView
的寬度大於此值,則Button
應與TextView
具有相同的寬度。
這應該是這個樣子:
Short Label
+-------------+
+-------------+
Very Long Label Text
+------------------+
+------------------+
我試着用的LinearLayout或RelativeLayout的不同解決方案根佈局,但問題始終是相同的:我是不是能夠適應Button
的寬度到TextView
的寬度。給予Button
150dp的最小寬度當然不成問題。但如何解決長標籤的第二種情況?
<SomeRootLayout ...>
<!-- also tried LinearLayout, but problem is the same... -->
<RelativeLayout
android:id="@+id/editTextAndLabelContainer"
...>
<TextView
android:id="@+id/labelTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A " />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/labelTV" />
</RelativeLayout>
</SomeRootLayout>
如果Button
有一個「match_parent」寬寬度不對齊的TextView
但設置爲完整的屏幕寬度。
我該如何解決這個問題?
通過getwidth()爲文本視圖執行編程...如果大於則按鈕的寬度與textview相同。 – Ranjit