gpt4 book ai didi

java - 在另一个 View 周围的圆圈中动态添加 View

转载 作者:行者123 更新时间:2023-11-30 03:43:02 24 4
gpt4 key购买 nike

我想在屏幕中央有一个 TextView ,并在圆圈的边缘周围放置一些自定义的 SeekBar。它应该看起来像这样。

How the view should look

这是我目前的代码。为了使用更少的代码而不是自定义的 SeekBars,我将使用默认的 SeekBars。我正在使用 Vertical Seek Bar 库 https://github.com/AndroSelva/Vertical-SeekBar-Android

主要 Activity :

public class MainActivity extends Activity implements OnSeekBarChangeListener {

TextView tv;
TextView ss;

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

RelativeLayout layout = (RelativeLayout) findViewById(R.id.main_layout);
tv = (TextView)findViewById(R.id.seek_score);
ss = (TextView)findViewById(R.id.selected_segment);


RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv.getId());

VerticalSeekBar verticalSeekBar = (VerticalSeekBar) getLayoutInflater().inflate(R.layout.motif_segment, null);
verticalSeekBar.setName("Test Segment");
verticalSeekBar.setOnSeekBarChangeListener(this);
layout.addView(verticalSeekBar, lp);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return false;
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean isUser) {
tv.setText(Integer.toString(progress / 100));
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {


}
}

ma​​in_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/main_layout"
android:layout_gravity="center"
>

<TextView
android:id="@+id/seek_score"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerHorizontal="true"
android:background="@drawable/circle"
android:gravity="center"
android:text="0"
android:layout_margin="40dip"
/>

<TextView
android:id="@+id/selected_segment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="" />

</RelativeLayout>

最佳答案

I want to have a text view in the center of a screen with a number of custom SeekBars positioned around the edges in a circle

如果编号的 View 具有任意尺寸(意思是它们比中心圆大)并且您只想像图像一样放置它们,那么这很简单:

  • 创建一个正方形的圆形 View ,边长为 4 和 2 或 3 和 1 之间的距离,以父级为中心
  • View 1 将水平居中 + 设置在圆形 View 上方
  • View 3 将水平居中 + 设置在圆形 View 下方
  • View 4 将垂直居中 + 设置在圆形 View 的左侧
  • View 2 将垂直居中 + 设置在圆形 View 的右侧

如果您希望 View 1、2、3、4 具有圆的大小,则使用上方的位置,但也使用圆 View 的 alignLeft/Right/top/Bottom 属性。

如果这四个 View 不是您想要放置在圆圈周围的单个 View ,那么您应该创建一个自定义的 ViewGroup 来更精确地计算应该如何放置 subview 。

关于java - 在另一个 View 周围的圆圈中动态添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15452036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com