【发布时间】:2016-11-21 16:07:17
【问题描述】:
我正在尝试构建这个布局
但我得到了这个:(该位置的地图按钮将是理想的高度和宽度,但文本视图与高度不匹配)
我的位置是TextView,我的目的地是EditText
我希望 TextView 和 EditText 的宽度相同,而 MAP 按钮保持“方形”。
如何让 textviews 高度匹配并制作方形地图按钮?
这是我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mycompany.controller.DetailsFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/viewStatus"
android:layout_marginTop="5dp">
<LinearLayout
android:id="@+id/scrollViewDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- LOCATION -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="LOCATION"
android:textColor="@color/COLOR_BLUE"
android:textSize="24sp"/>
<LinearLayout
android:id="@+id/layoutLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvLocation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight=".8"
android:background="@color/COLOR_LIGHT_GREY"
android:text="123 MAIN ST., CHATTANOOGA TN 37404"
android:textSize="24sp"/>
<Button
android:id="@+id/btnMapLocation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="@color/COLOR_BLUE"
android:text="MAP"
android:textColor="@color/COLOR_WHITE"/>
</LinearLayout>
<!-- DESTINATION -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="DESTINATION"
android:textColor="@color/COLOR_BLUE"
android:textSize="24sp"/>
<LinearLayout
android:id="@+id/layoutDestination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editDestination"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".8"
android:ems="10"
android:inputType="textMultiLine"
android:text="407 Broad St., Anywhere ST 00000"/>
<Button
android:id="@+id/btnMapDestination"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="@color/COLOR_BLUE"
android:text="MAP"
android:textColor="@color/COLOR_WHITE"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
【问题讨论】:
-
使用权重永远不能保证事物是正方形的,关键是它们会拉伸以填充与布局方向相关的特定大小(在这种情况下是水平的)。如果您希望某些东西是方形的,则需要明确设置宽度和高度,或者您需要在绘制视图后获取测量的高度并将宽度设置为匹配。
标签: java android xml android-layout android-linearlayout