【问题标题】:PercentRelativeLayout okay on Emulator, not okay on phonePercentRelativeLayout 在模拟器上可以,在手机上不行
【发布时间】:2016-07-03 19:57:57
【问题描述】:

我刚刚实现了PercentRelativeLayout。我在 gradle 中添加了:compile 'com.android.support:percent:24.2.0',因此 Android Studio 至少可以识别视图。并添加了 Google API。

我遇到了渲染问题。在模拟器中,我的显示器工作正常。安装在运行 Android 4ish 的旧手机上,出现 TextViews 但不显示图像视图。

在预览渲染中(在设计选项卡中),当我切换到 API 23 而不是 API 24 时,ImageViews 会显示。

我觉得这只是工作。对此有任何已知的修复吗?不幸的是,视图对于缩放我的布局至关重要。

编辑:预览突然又重新启动了几次 android studio。没有改变其他任何东西。

该布局在预览版中一直有效,直至 API 16,但仍无法在手机上运行。 ImageViews 不显示。

XML 下面

<LinearLayout 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="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.y"
tools:showIn="@layout/app_bar_main"
android:background="#FFF"
android:weightSum="1"
android:orientation="vertical">

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    android:scrollIndicators="top">

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="This text appears correctly"
            android:textSize="28sp"
            android:id="@+id/textMainTitle"
            android:layout_gravity="center"
            android:layout_alignParentTop="true"
            android:textColor="#ffffff"
            android:singleLine="true"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="As does this text"
            android:id="@+id/textMainSubTitle"
            android:layout_gravity="center_horizontal"
            android:layout_below="@id/textMainTitle"
            />


        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btnIntro"
            android:src="@drawable/button1" //DOES NOT APPEAR IN PHONE
            app:layout_widthPercent="30%"
            app:layout_heightPercent="25%"
            android:layout_below="@id/textMainSubTitle"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp" />

   </android.support.percent.PercentRelativeLayout>

 </ScrollView>


</LinearLayout>

【问题讨论】:

  • 如果您需要帮助调试布局的 xml,您必须包含您的布局 xml。

标签: android android-layout android-studio


【解决方案1】:

终于想通了。问题实际上出在 XML 中。在这里发布以供参考,希望将来有人会发现它有帮助。

问题来自设置layout_heightPercentlayout_widthPercent。由于某种原因,这不允许显示相关视图。我不认为这与缩放纵横比有任何关系,因为按钮也存在问题(没有需要保持纵横比。)

但如果您在使用 PercentRelativeLayout 时遇到可见性问题,请参见下文。

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/btnIntro"
        android:src="@drawable/button1" 
        app:layout_widthPercent="30%"       // MUST SET ONE 
        app:layout_heightPercent="25%"      // OR OTHER NOT BOTH
        android:layout_below="@id/textMainSubTitle"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp" />

【讨论】:

    【解决方案2】:

    受 BR89 启发,我的问题的解决方案实际上是使用“android:src”而不是“app:srcCompat”,并在“android:layout_width”上指定“0dp”

    但是在我的物理设备上同时放置“app:layout_widthPercent”和“app:layout_heightPercent”不会有任何问题。

    <android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            app:layout_marginTopPercent="25%"
            app:layout_marginLeftPercent="25%"
            android:src="@drawable/logo"/>
    </android.support.percent.PercentRelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2023-04-10
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多