首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式在ImageView上放置标签

以编程方式在ImageView上放置标签
EN

Stack Overflow用户
提问于 2016-10-13 10:23:46
回答 2查看 434关注 0票数 2

我正在做一个应用程序来解决sudokus,所以在应用程序中,我使用了一个Imageview,它显示了一个空的9x9网格,我已经将其放置在相对布局中。我正在尝试以编程方式创建界面,所以我想知道的是如何将标签放在Imageview的顶部,以便它们与网格的方块位置相匹配。我知道标签可以放在Imageview的顶部,但不知道如何将它们放在适当的位置。Xamarin是否提供了一种方法来实现这一点,因为我发现的所有方法都是如何将对齐方法与相对布局一起使用。

以下是我到目前为止的代码:

代码语言:javascript
运行
复制
public class GridActivity : Activity
{
    // Global Variables
    Button startButton;
    Button nextButton;
    RelativeLayout.LayoutParams startButtonParamsPortrait;
    RelativeLayout.LayoutParams startButtonParamsLandscape;
    RelativeLayout.LayoutParams nextButtonParamsPortrait;
    RelativeLayout.LayoutParams nextButtonParamsLandscape;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        // Don't want the action bar to show, find it unnecessary, so gonna hide it.
        ActionBar.Hide();

        base.OnCreate(savedInstanceState);

        // Create your application here
        // get the initial orientation
        var surfaceOrientation = WindowManager.DefaultDisplay.Rotation;

        RelativeLayout layoutBase = new RelativeLayout(this);
        layoutBase.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
        layoutBase.SetPadding(100, 100, 100, 100);

        // Adding a Imageview to display the sudoku grid
        ImageView grid = new ImageView(this);
        grid.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        grid.Visibility = ViewStates.Visible;
        grid.SetBackgroundResource(Resource.Drawable.SudokuGrid);
        grid.Id = 1;
        layoutBase.AddView(grid);

        // Adding a button that will be used to step through the "AI"'s solution
        nextButton = new Button(this) { Text = "Next" };
        nextButton.Id = 2;

        // Adding a button that will be used to start the "AI" to solve the puzzle
        startButton = new Button(this) { Text = "Start" };
        startButton.Id = 3;

        // Layout Parameters for Portrait mode
        // nextButton
        nextButtonParamsPortrait = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        nextButtonParamsPortrait.AddRule(LayoutRules.AlignParentBottom);
        nextButtonParamsPortrait.AddRule(LayoutRules.AlignParentRight);
        // startButton
        startButtonParamsPortrait = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        startButtonParamsPortrait.AddRule(LayoutRules.AlignParentBottom);
        startButtonParamsPortrait.AddRule(LayoutRules.LeftOf, nextButton.Id);

        // Layout Parameters for Landscape mode
        // startButton
        startButtonParamsLandscape = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        startButtonParamsLandscape.AddRule(LayoutRules.AlignParentRight);
        // nextButton
        nextButtonParamsLandscape = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        nextButtonParamsLandscape.AddRule(LayoutRules.AlignParentRight);
        nextButtonParamsLandscape.AddRule(LayoutRules.Below, startButton.Id);

        // Add labels in the location of the squares 

        // Depending on the initial orientation, the buttons will placed at different locations
        if (surfaceOrientation == SurfaceOrientation.Rotation0 || surfaceOrientation == SurfaceOrientation.Rotation180)
        {
            // The screen is in Portrait mode
            startButton.LayoutParameters = startButtonParamsPortrait;
            nextButton.LayoutParameters = nextButtonParamsPortrait;
        }
        else
        {
            // The screen is in Landscape mode
            startButton.LayoutParameters = startButtonParamsLandscape;
            nextButton.LayoutParameters = nextButtonParamsLandscape;
        }

        // Add the buttons to the layout
        layoutBase.AddView(startButton);
        layoutBase.AddView(nextButton);

        // Display the layout to the screen
        SetContentView(layoutBase);            
    }

    public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
    {
        base.OnConfigurationChanged(newConfig);

        if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
        {
            startButton.LayoutParameters = startButtonParamsPortrait;
            nextButton.LayoutParameters = nextButtonParamsPortrait;
        }
        else if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
        {
            startButton.LayoutParameters = startButtonParamsLandscape;
            nextButton.LayoutParameters = nextButtonParamsLandscape;
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2016-10-13 20:03:52

您可以使用绝对布局将标签放置在布局的其他子项(您的网格)上(必须是较年长的子项)。

票数 0
EN

Stack Overflow用户

发布于 2016-10-13 12:27:03

您可以使用framelayout来实现这一点。

下面是相同的简单布局。

代码语言:javascript
运行
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
<ImageView 
   android:src="@drawable/ic_launcher"  
   android:scaleType="fitCenter"
   android:layout_height="250px"
   android:layout_width="250px"/>
<TextView
   android:text="Frame Demo"
   android:textSize="30px"
   android:textStyle="bold"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"
   android:gravity="center"/>
</FrameLayout>

您也可以通过编程方式在中添加相同的TextView。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40011280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档