【问题标题】:How to put TextureView between other Layouts in Android correctly?如何正确地将TextureView放在Android中的其他布局之间?
【发布时间】:2014-10-23 13:00:42
【问题描述】:

我在将 GLSurfaceView 放在其他布局之间时遇到了问题,事实证明 - GLSurfaceView 不可能。所以我用TextureView替换了GLSurfaceView,但是问题没有解决。

我的主要布局中有这样的结构:

<RelativeLayout android:id="@+id/mainEngineLayout"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:background="@android:color/transparent"
                tools:context=".activities.MyActivity">

    <com.myapplication.views.openGL.myGLTextureView
            android:id="@+id/myGLTextureView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="66dp"
            android:layout_marginRight="66dp"/>

    <LinearLayout
            android:id="@+id/leftButtonsLayout"
            android:layout_height="fill_parent"
            android:layout_width="66dp"
            android:layout_alignParentLeft="true"
            android:orientation="vertical"
            android:background="#b7b8b0">

        <ImageButton android:layout_width="66dp"  .../>
        <ImageButton android:layout_width="66dp"  .../>

    </LinearLayout>

    <FrameLayout
            android:layout_height="fill_parent"
            android:id="@+id/rightLayout"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:orientation="vertical"
            android:background="#b7b8b0">

        <ImageButton android:layout_width="66dp"  .../>
        <ImageButton android:layout_width="66dp"  .../>

    </FrameLayout>    

</RelativeLayout>

最后我想得到如下结果:

问题是在某些设备上我遇到了根本不显示 TextureView 的问题,因此左右布局之间存在黑场。

例如,它可以在我的三星 S3 和模拟器中运行,但不能在 Xperia 上正常运行。 我认为原因可能是不同的主题,但我在清单文件中设置了主题,例如:

<application
    android:allowBackup="true"
    android:configChanges="orientation|screenSize"
    android:theme="@style/CustomHoloThemeNoActionBar"
    android:screenOrientation="sensorLandscape"
    android:largeHeap="true">
    <activity
        android:theme="@style/CustomHoloThemeNoActionBar"

在哪里"CustomHoloThemeNoActionBar" parent="@android:style/Theme.Holo.Light"

您能否告知可能导致此类问题的原因?

【问题讨论】:

  • 我认为你可以使用 android:layout_toRightOf 来让你的布局正确...和 ​​android:layout_toLeftOf 让你的布局左...。用花药布局 id 替换点
  • 在使用 layout_toRightOf 和 layout_toLeftOf 的情况下,我必须将 myGLTextureView 放置在我不需要的“左”和“右”布局之后,我希望将 TextureView 作为第一个布局,例如背景。
  • android:gravity="center"
  • 这个不错的链接会帮助你sandipchitale.blogspot.in/2010/05/…
  • @minafawzy 为什么您认为不喜欢使用“dp”值?我认为“dp”的意思是“与密度无关的像素”,它假设尺寸不取决于显示密度。

标签: android android-layout glsurfaceview textureview


【解决方案1】:

我认为您应该将重力或 (layout_gravity) 设置为 center ,

这取决于你想要的样子

如果你想划分它们来固定百分比,你可以使用 weight 属性

【讨论】:

  • 不幸的是它没有帮助。其实我不明白为什么重力会影响 TextureView 被其他布局“重叠”的问题
  • 然后尝试 android:layout_centerInParent="true" 或 android:layout_centerHorizo​​ntal="true" 我希望这对你有用
【解决方案2】:

我不知道 OpenGLTextureView,但对于您的最后一个问题,我会尝试将 TextureView 放在 FrameLayout 中。

我会更改您的 XML 的结构,例如 (without fill_parent 在 API 级别 8 及更高版本中已弃用并重命名为 ma​​tch_parent,但它与我认为你的问题):

<RelativeLayout android:id="@+id/mainEngineLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                tools:context=".activities.MyActivity">

    <LinearLayout
            android:id="@+id/horizontalContainer"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:background="#b7b8b0">

       <LinearLayout
               android:id="@+id/leftButtonsLayout"
               android:layout_height="match_parent"
               android:layout_width="wrap_content"
               android:orientation="vertical"
               android:background="#b7b8b0">

             <ImageButton android:layout_width="match_parent"  .../>
             <ImageButton android:layout_width="match_parent"  .../>

        </LinearLayout>

        <FrameLayout
               android:layout_height="match_parent"
               android:id="@+id/textureContainer"
               android:layout_width="match_parent"
               android:background="#b7b8b0">

               <TextureView
                  android:id="@+id/myGLTextureView"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  />
        </FrameLayout> 
        <FrameLayout
               android:layout_height="match_parent"
               android:id="@+id/rightLayout"
               android:layout_width="wrap_content"
               android:orientation="vertical"
               android:background="#b7b8b0">

             <ImageButton android:layout_width="match_parent"  .../>
             <ImageButton android:layout_width="match_parent"  .../>

         </FrameLayout>    
     </LinearLayout>

</RelativeLayout>

但我建议您使用 java 代码来使项目大小动态化。例如,在您的活动中,这将给出如下内容:

private LinearLayout leftLayoutButtonContainer;
private FrameLayout rightLayoutButtonContainer;

private TextureView textureView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        leftLayoutButtonContainer = findViewById(R.id.leftButtonsLayout);
        rightLayoutButtonContainer = findViewById(R.id.rightLayout);

        textureView = findViewById(R.id.myGLTextureView);

        // Screen dimensions
        display = getWindowManager().getDefaultDisplay();
        size = new Point();
        // Get the screen dimensions between the status bar & the navigation bar.
        display.getSize(size);
        //display.getRealSize(size) if you want to use the size in full screen mode
        // This means without the status and navigation bar counted in the screen size.

       //Get the screen dimensions
       maxWidth = size.x 
       maxHeight = size.y;

       // Call this method in the onCreate() of your activity
       // If you are using Fragment, inside the onActivityCreated() method
       // Which is called after onCreateView() so your items will not be null objects
       setupLayouts();
   }


private void setupLayouts() {

    // If you trully need 66dp, you can let it in XML for your buttons 
    // Then just set the TextureView size such as below
    textureView.getLayoutParams.width = maxWidth - 2 * 66;

    // If you want to set everything dynamically : comment the above line
    // 15% of the width of the screen for both container, so you will have 70% of the screen for your textureView 
    //(from what i see on the picture representing what you want)
    // If you choose the below option, you need to set your Buttons in match_parent in your XML file
    leftLayoutButtonContainer.getLayoutParams.width = maxWidth * 15 / 100; 
    rightLayoutButtonContainer.getLayoutParams.width = maxWidth * 15 / 100;
    textureView.getLayoutParams.width = maxWidth * 70 / 100;


}

说明:

一般的想法是在您的 XML 中放置正确的布局方向,这将处理元素的“顺序”。

这就是 LinearLayoutandroid:orientation="horizontal" 的主要作用。

然后,您想使用match_parent 属性设置所有元素并使用% 设置您想要的项目的大小。

这是 XML 中属性权重背后的想法和可能的数学,但我从不这样做,因为我有时喜欢用我的数学随时控制我的项目。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多