2017-03-16 46 views
0

我已经包含与LinearLayout中的比重设定为 “中心”,显示在中心以下部件垂直的LinearLayout:调整的ImageView的ViewBounds当父布局内容溢出

  • ImageView的
  • TextInputLayout
  • TextInputLayout
  • 按钮

在小屏幕设备上时,这看起来很正常s,底部的按钮即将熄灭。当我为两个TextInputLayouts显示错误时,情况甚至更糟。

截图标准屏幕:

On Normal Screens

截图小屏幕:

On Small Screens

问题:

我想要的是,在小屏幕上,ImageView应该调整自己以容纳所有视图,而在更大的屏幕上,ImageView应保持其最大可用大小,同时所有内容都显示在中心(如截图1所示)。

任何帮助将不胜感激! :)

源XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/input_view_margin" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/ic_transaction_history_big" /> 

    <TextView 
     style="@style/ReportHeading" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/txt_transaction_history" /> 

    <android.support.v4.widget.Space 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" /> 

    <TextInputLayout 
     android:id="@+id/wrapperFromDate" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextInputEditText 
      android:id="@+id/edtFromDate" 
      style="@style/InputLayoutEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:hint="@string/hint_from_date" /> 
    </TextInputLayout> 

    <TextInputLayout 
     android:id="@+id/wrapperToDate" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp"> 

     <TextInputEditText 
      android:id="@+id/edtToDate" 
      style="@style/InputLayoutEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:hint="@string/hint_to_date" /> 
    </TextInputLayout> 

    <Button 
     android:id="@+id/btnSubmit" 
     style="@style/PrimaryButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="35dp" 
     android:text="@string/action_submit" /> 
</LinearLayout> 
+0

你是否用xml修复了你的视图的大小? 张贴您的xml? –

+0

给视图赋予权重。然后使用scaleType调整图像的大小。 – Rachit

回答

0

您可以滚动型添加到您的XML喜欢本作的用户能够看到您的按钮

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/input_view_margin" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/ic_transaction_history_big" /> 

    <TextView 
     style="@style/ReportHeading" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/txt_transaction_history" /> 

    <android.support.v4.widget.Space 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" /> 

    <TextInputLayout 
     android:id="@+id/wrapperFromDate" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextInputEditText 
      android:id="@+id/edtFromDate" 
      style="@style/InputLayoutEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:hint="@string/hint_from_date" /> 
    </TextInputLayout> 

    <TextInputLayout 
     android:id="@+id/wrapperToDate" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp"> 

     <TextInputEditText 
      android:id="@+id/edtToDate" 
      style="@style/InputLayoutEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:hint="@string/hint_to_date" /> 
    </TextInputLayout> 

    <Button 
     android:id="@+id/btnSubmit" 
     style="@style/PrimaryButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="35dp" 
     android:text="@string/action_submit" /> 
</LinearLayout> 
</ScrollView> 
+0

我不想添加ScrollView。如果ImageView占用一点空间,我会有一个完美的视图。 – Umair

0

给你的意见和重量设置宽度高度以匹配父级布局的父级,为子级视图赋予权重。请记住将高度设置为0dp并将宽度设置为0dp,以便根据重量进行调整。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/input_view_margin" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     app:srcCompat="@drawable/ic_transaction_history_big" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      style="@style/ReportHeading" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/txt_transaction_history" /> 

     <android.support.v4.widget.Space 
      android:layout_width="wrap_content" 
      android:layout_height="40dp" /> 

     <TextInputLayout 
      android:id="@+id/wrapperFromDate" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextInputEditText 
       android:id="@+id/edtFromDate" 
       style="@style/InputLayoutEditText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:focusable="false" 
       android:hint="@string/hint_from_date" /> 
     </TextInputLayout> 

     <TextInputLayout 
      android:id="@+id/wrapperToDate" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp"> 

      <TextInputEditText 
       android:id="@+id/edtToDate" 
       style="@style/InputLayoutEditText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:focusable="false" 
       android:hint="@string/hint_to_date" /> 
     </TextInputLayout> 

     <Button 
      android:id="@+id/btnSubmit" 
      style="@style/PrimaryButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="35dp" 
      android:text="@string/action_submit" /> 

    </LinearLayout> 

</LinearLayout> 

它会解决你的问题,让我知道它是否工作。祝你好运!

0

对“flex views”使用ConstraintLayout,因此您可以为元素设置灵活的约束。 PS:你可以在android studio中轻松测试你的实现,当你展开时,缩小预览/设计中的屏幕尺寸。