【问题标题】:LinearLayout and FrameLayout for positioning buttonsLinearLayout 和 FrameLayout 用于定位按钮
【发布时间】:2014-06-30 10:52:09
【问题描述】:

我已经尝试了几个小时自己解决这个问题,但无法做到。这就是我想要做的:Image of desired layout

“蓝色”矩形是 TextSwitcher 应该包含的区域,但我无法将“重做集”直接放在“上一个项目”上方,将“下一个设置”直接放在“下一个项目”上方“ 按钮。这是我的布局 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Spinner
        android:id="@+id/spinner_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <TextSwitcher
    android:id="@+id/textSwitcherMain"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="40sp"
    android:keepScreenOn="true"
    android:text="" >

    </TextSwitcher>

    <Button
    android:id="@+id/problematicButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|top"
    android:visibility="visible"
    android:text="@string/problematic" />

    <Button
    android:id="@+id/playButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|top"
    android:visibility="visible"
    android:text="@string/play_1" />

    <Button
    android:id="@+id/nextSetButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:visibility="visible"
    android:text="@string/next_set" />

    <Button
    android:id="@+id/redoSetButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|bottom"
    android:visibility="visible"
    android:text="@string/redo_set" />

</FrameLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
    android:id="@+id/previousButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:text="@string/previous_item_button" />

    <Button
    android:id="@+id/nextButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:text="@string/next_item_button" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <com.google.android.gms.ads.AdView 
    ads:adUnitId="**************"
    ads:adSize="SMART_BANNER"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:gravity="center"
    android:layout_gravity="center" />

</LinearLayout>

我尝试将 nextButton 和 previousButton 放在 FrameLayout 中,但随后它们分别与 redoSetButton 和 nextSetButton 重叠。此外,nextButton 和 previousButton 应该各自占据 50% 的宽度,所以我不得不将其放入 LinearLayout 中,并尝试将各种组合放入 FrameLayout 中,但我无法以这种方式定位这些按钮。 此外,所有这些按钮都位于 TextSwitcher 的顶部很重要,因为它将具有应该在按钮后面可见的背景图像/颜色。

这是我的第一篇文章。我希望我已经足够详细了。感谢您提前输入。

【问题讨论】:

    标签: java android android-layout button position


    【解决方案1】:

    试试这个方法,希望能帮助你解决问题。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Spinner
                android:id="@+id/spinner_1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
    
            <Spinner
                android:id="@+id/spinner_2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
    
            <Spinner
                android:id="@+id/spinner_3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
    
        </LinearLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
    
            <TextSwitcher
                android:id="@+id/textSwitcherMain"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="40sp"
                android:keepScreenOn="true"
                android:text="" >
    
            </TextSwitcher>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top">
    
                <Button
                    android:id="@+id/problematicButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/problematic" />
    
                <Button
                    android:id="@+id/playButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/play_1" />
    
                </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom">
            <Button
                android:id="@+id/nextSetButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/next_set" />
    
            <Button
                android:id="@+id/redoSetButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/redo_set" />
            </LinearLayout>
    
        </FrameLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/previousButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/previous_item_button" />
    
            <Button
                android:id="@+id/nextButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/next_item_button" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <com.google.android.gms.ads.AdView
                ads:adUnitId="**************"
                ads:adSize="SMART_BANNER"
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center" />
    
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • "Previous Item" 和 "Next Item" 必须在 TextSwitcher 上绘制,不幸的是这里不是这样,因为它们现在显示在 TextSwitcher 区域下方。我知道可能很难理解我的意思。此外,redoSetButton/nextSetButton/problematicButton/playButton 应该保持(大小)相同(如在我的代码中),所以我不希望它们填满整个宽度。按照你的说法,它们各占宽度的 50%。不过感谢您的努力。
    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    相关资源
    最近更新 更多