【问题标题】:Android seekbars cut off/clipped on rotation in FrameLayoutAndroid 搜索栏在 FrameLayout 中旋转时被切断/剪切
【发布时间】:2017-05-26 05:24:26
【问题描述】:

所以我正在尝试制作一个穷人的交互式雷达图或蜘蛛图,但我找不到。我确实找到了这个: Android Layout views rotated and spaced around a circle? 并考虑在其中旋转搜索栏,它有点工作,但搜索栏本身被一些边界剪裁/截断,我不知道它是什么。我尝试了一些使用填充的方法,但它们没有帮助。

MainActivity 代码如下所示:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Context context = getApplicationContext();

    final FrameLayout main = (FrameLayout)findViewById(R.id.main);

    int numViews = 6;
    for(int i = 0; i < numViews; i++)
    {
        // Create some quick TextViews that can be placed.
        TextView v = new TextView(this);
        SeekBar sb = new SeekBar(this);

        // Set a text and center it in each view.
        v.setText("View " + i);

        v.setGravity(Gravity.CENTER);
        v.setBackgroundColor(0xff00ffff);
        // Force the views to a nice size (150x100 px) that fits my display.
        // This should of course be done in a display size independent way.
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(400, 75);
        FrameLayout.LayoutParams lpv = new FrameLayout.LayoutParams(300, 75);

        // Place all views in the center of the layout. We'll transform them
        // away from there in the code below.
        lp.gravity = Gravity.CENTER;
        lpv.gravity = Gravity.CENTER;

        lp.bottomMargin = 150;
        lpv.bottomMargin = 150;

        // Set layout params on view.
        v.setLayoutParams(lpv);
        sb.setLayoutParams(lp);

        // Calculate the angle of the current view. Adjust by 90 degrees to
        // get View 0 at the top. We need the angle in degrees and radians.
        float angleDeg = i * 360.0f / numViews - 90.0f;
        float angleRad = (float)(angleDeg * Math.PI / 180.0f);
        // Calculate the position of the view, offset from center (300 px from
        // center). Again, this should be done in a display size independent way.
        v.setTranslationX(500 * (float)Math.cos(angleRad));
        v.setTranslationY(500 * (float)Math.sin(angleRad));

        sb.setTranslationX(250 * (float)Math.cos(angleRad));
        sb.setTranslationY(250 * (float)Math.sin(angleRad));
        // Set the rotation of the view.
        v.setRotation(angleDeg +90f);

        sb.setRotation(angleDeg );
        main.addView(sb);
        main.addView(v);
    }
}

而activity_main.xml 代码是:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Settings"
android:layout_marginBottom="150px"/>
</FrameLayout>

任何水平(如果有 8 个)或垂直的搜索栏看起来都很好,但对角线的搜索栏显示不正确。结果是这样的:

所以视图 1 和 2 几乎不显示条形图,尽管拇指都正确移动。视图 4 和 5 根本不显示。我应该改变什么以使它们完全可见?非常感谢!

编辑:添加 numViews= 8 和 15 的屏幕截图

奇怪的是,右侧只有一些对角线可见,左侧什么都没有。是否也存在某种需要旋转的填充“框”?

【问题讨论】:

  • 我正在使用带有最新更新的 Nexus 5x... 7.1.2
  • 您找到解决方案了吗?我也在寻找类似的东西。
  • 不,我还没有找到解决方案,抱歉:/

标签: android android-layout rotation seekbar


【解决方案1】:

我最近也遇到了这个问题。但是我正在使用 Android Studio 中的布局编辑器构建我的 UI,并且想知道为什么旋转的搜索栏在预览中看起来不错。所以我尝试添加一个软件layerType,这似乎有效!

android:layerType="software"

或者在你的情况下:

sb.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

希望这会有所帮助。

Screenshot

【讨论】:

  • 是的,做到了! :) 你能解释一下你是怎么发现的吗?或者粘贴一个链接?谢谢!
  • 其实只是偶然发现的。我注意到搜索栏在 Android Studio 的布局编辑器中正确呈现,所以我决定尝试一下 :)
【解决方案2】:

安卓设备不同屏幕分辨率和密度的问题。所以请阅读这个 “支持多个屏幕”阅读此站点: https://developer.android.com/guide/practices/screens_support.html

【讨论】:

  • 我知道我已经把它硬编码到我的屏幕上,接下来我会修复它,谢谢你的链接....但是有什么关于切断部分元素的吗?
  • 从 Supporting Multiple Screens 开始,您应该像这样定义布局文件夹 res/layout/my_layout.xml // 正常屏幕尺寸的布局(“默认”) res/layout-large/my_layout.xml //大屏布局 res/layout-xlarge/my_layout.xml // 超大屏布局 res/layout-xlarge-land/my_layout.xml
【解决方案3】:

我在 2 个模拟器上运行了你的代码,看起来不错 -

您在哪个 Android 版本上运行?

【讨论】:

  • 你支持多屏吗?
  • @SantoshBachkar 不确定我是否理解您的问题
  • 你了解@SimplyJaymin
  • @SimplyJaymin 我正在使用带有最新更新的 Nexus 5x... 7.1.2
【解决方案4】:

manifast xml文件中还需要支持不同的屏幕打开“AndroidManifest”,在android versionName后面添加以下内容。

<supports-screens android:smallScreens="true"

                android:normalScreens="true"

                android:largeScreens="true"

                android:xlargeScreens="true"

                android:anyDensity="true"

                android:resizeable="true"/>

【讨论】:

  • 你好 Santosh,我试过了,但没有帮助。我现在只需要一种屏幕尺寸即可工作。我的问题是对角线有一个“不可见的矩形”。有没有特别适用的部分?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多