Android 以编程方式在ImageView上放置标签
我正在制作一个应用程序来解决数独问题,所以在这个应用程序中,我使用了一个Imageview,它显示了一个空的9x9网格,我把它放在了一个相对的布局中。我正在尝试以编程方式创建接口,所以我想知道的是如何将标签放置在Imageview的顶部,以便它们与网格正方形的位置相匹配。我知道标签可以放在Imageview的顶部,但不知道如何将它们放置到位。Xamarin是否提供了一种实现这一点的方法,因为我所发现的只是如何在相对布局中使用对齐方法 以下是迄今为止我的代码:Android 以编程方式在ImageView上放置标签,android,xamarin,android-6.0-marshmallow,Android,Xamarin,Android 6.0 Marshmallow,我正在制作一个应用程序来解决数独问题,所以在这个应用程序中,我使用了一个Imageview,它显示了一个空的9x9网格,我把它放在了一个相对的布局中。我正在尝试以编程方式创建接口,所以我想知道的是如何将标签放置在Imageview的顶部,以便它们与网格正方形的位置相匹配。我知道标签可以放在Imageview的顶部,但不知道如何将它们放置到位。Xamarin是否提供了一种实现这一点的方法,因为我所发现的只是如何在相对布局中使用对齐方法 以下是迄今为止我的代码: public class GridA
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;
}
}
您可以使用来实现这一点
下面是简单的布局,以供参考
<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
您可以使用来实现这一点
下面是简单的布局,以供参考
<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
您可以使用绝对布局将标签放置在布局的其他子项(您的网格)上(必须是较年长的子项)。您可以使用绝对布局将标签放置在布局的其他子项(您的网格)上(必须是较年长的子项)。标签的用途是什么?你能通过改变ImageView中的图像来完成同样的事情吗?标签的目的是显示数独的数字,它们还有一个单击的方法,该方法将显示一个“选择器”,允许用户选择1-9之间的数字,以便设置应用程序要解的谜题。我不想使用文本视图,因为它们没有我想要的平面文本外观。标签的用途是什么?你能通过改变ImageView中的图像来完成同样的事情吗?标签的目的是显示数独的数字,它们还有一个单击的方法,该方法将显示一个“选择器”,允许用户选择1-9之间的数字,以便设置应用程序要解的谜题。我不想使用文本视图,因为它们没有我想要的平面文本外观。今天晚些时候,当我有更多的空闲时间时,我会研究这个布局。我假设相对布局没有办法做到这一点,这就是为什么你建议使用这种布局。你也可以看看关于这两种布局利弊的讨论——示例可以让你知道如何使用相对布局。今天晚些时候,当我有更多的空闲时间时,我会研究这种布局。我假设相对布局没有办法做到这一点,这就是为什么你建议使用这种布局。你也可以看看关于这两种布局利弊的讨论——示例也可以让你知道如何使用相对布局