如何在Android中的其他布局之间正确放置TextureView?

时间:2014-10-23 13:00:43

标签: android android-layout glsurfaceview textureview

我遇到了将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>

最后我希望获得如下结果:

enter image description here

问题在于,在某些设备上我完全没有显示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"

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

2 个答案:

答案 0 :(得分:1)

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

我会更改您XML的结构,例如(不使用 fill_parent ,该API在第8级及更高版本中已弃用并重命名为 match_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属性权重背后的数学,但我从来没有这么做,因为我有时有时想通过我的数学控制我的项目。

希望这会有所帮助!

答案 1 :(得分:0)

我认为你应该将引力或(layout_gravity)设置为居中,

它依赖于你想要的样子

enter image description here

如果你想将它们分成固定百分比,你可以使用重量属性

enter image description here