android中获取TextView位置

android中获取TextView位置,第1张

TextView tv=new TextView(context);

LinearLayoutLayoutParams params=new LinearLayoutLayoutParams(

LinearLayoutLayoutParamsWRAP_CONTENT,LinearLayoutLayoutParamsWRAP_CONTENT

);

tvsetLayoutParams(params);

//左右上下

int left=tvgetLeft();

int right=tvgetRight();

int width=tvgetWidth();

int height=tvgetHeight();

//左右上下的内部空白

int padingLeft=tvgetPaddingLeft();

int padingRight=tvgetPaddingRight();

int padingTop=tvgetPaddingTop();

int padingBt=tvgetPaddingBottom();

//只有LinearLayoutLayoutParams等有Margins属性,不是所有的布局都有Margins要看具体情况

int marginLeft=paramsleftMargin;

int marginRight=paramsrightMargin;

int marginTop=paramstopMargin;

int marginBt=paramsbottomMargin;

有时候运行时代码需要这样取Margins:

ViewGroupLayoutParams p=thisgetLayoutParams();

if(p!=null && p instanceof LinearLayoutLayoutParams){

LinearLayoutLayoutParams lp=(LinearLayoutLayoutParams)p;

int margingLeft=lpleftMargin;

}

1UITableViews存储他们的NSIndexPath。因此存在对部分没有对象。使用下面的代码就可以遍历表并执行的可见部分索引(我不知道你为什么想要看到的部分,因为他们看到,目前在屏幕上,但不管)。

for (NSIndexPath i in [yourTableViewName indexPathsForVisibleRows])

{

NSUInteger sectionPath = [i indexAtPosition:0];

//custom code here, will run multiple times per section for each visible row in the group

}

2

或者非常简单的方法是采取valueForKeyPath和的NSSet类的优势: NSSet visibleSections = [NSSet setWithArray:[[selftableView indexPathsForVisibleRows] valueForKey:@"section"]];

基本上,你在可见的行的部分值的数组,然后填入一组与此删除重复。

3

我已经得到了解决。 第一步,每个部分会显示,创建一个UIView- (UIView )tableView:(UITableView

)tableView viewForHeaderInSection:(NSInteger)section,将被存储到数组中。

当TableView中滚动,我想免费的无形剖面图,所以我需要知道哪些部分是可见或不可见,请按功能代码会检测这个目的,如果视图是可见的,然后释放它。 -(BOOL)isVisibleRect:(CGRect)rect containerView:(UIScrollView)containerView

{

CGPoint point = containerViewcontentOffset;

CGFloat zy = pointy ;

CGFloat py = rectoriginy + rectsizeheight;

if (py - zy <0) {

return FALSE;

}

CGRect screenRect = containerViewframe;

CGFloat by = screenRectsizeheight + zy ;

if (rectoriginy > by) {

return FALSE;

}

return TRUE;

}

(rect是该部分的UIView;containerView是UITableView)

通过这种方式,我可以得到的可见部分UITableView,但我希望在SDK可以用于此目的直接提供的API。

4 从可见的行列表中提取部分: NSArray indexPathsForVisibleRows = [tableView indexPathsForVisibleRows];

NSMutableIndexSet indexSet = [NSMutableIndexSet indexSet];

for ( NSIndexPath indexPath in indexPathsForVisibleRows ) {

[indexSet addIndex:indexPathsection];

}

NSLog(@"indexSet %@",indexSet);

// indexSet [number of indexes: 5 (in 1 ranges), indexes: (9-13)]

或: NSArray indexPathsForVisibleRows = [detailTableView indexPathsForVisibleRows];

NSMutableSet sectionSet = [NSMutableSet set];

for ( NSIndexPath indexPath in indexPathsForVisibleRows ) {

[sectionSet addObject:[NSNumber numberWithInt:indexPathsection]];

}

NSLog(@"sectionSet %@",sectionSet);

// sectionSet {(13, 11, 9, 10, 12 )}

5 另一种解决方案,可以使用在你的节头视图中的标签1位,这样的 #define _TBL_TAG_SECTION(_TAG) ((_TAG)|(1<<30))

#define _TBL_TAG_CLEAR(_TAG) ((_TAG)&((1<<30)-1))

#define _TBL_TAG_IS_SECTION(_TAG) ((_TAG)>>30)

- (UIView)tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section

{

// alloc header view

UIView header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

headertag = _TBL_TAG_SECTION(section);

return header;

}

- (void)scrollViewDidScroll:(UIScrollView )scrollView

{

CGRect r = CGRectMake(scrollViewcontentOffsetx, scrollViewcontentOffsety,

CGRectGetWidth(scrollViewframe),

CGRectGetHeight(scrollViewframe));

for (UIView v in [_tableView subviews]) {

if ( CGRectIntersectsRect(r, vframe) ) {

if ( _TBL_TAG_IS_SECTION(vtag) ) {

NSLog(@"visible section tag %d", _TBL_TAG_CLEAR(vtag));

}

}

}

}

6 2步解决方案,以获得在一个UITableView可见部分:

1)添加标题视图一个可变数组viewForHeaderInSection2)更新数组时,在滚动的tableviewscrollViewDidScroll注Tag属性来保存部分的数量

@property (nonatomic, strong, readwrite) NSMutableArray headerArray;

- (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section {

UIView headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableViewboundssizewidth, 40)];

headerViewbackgroundColor = [UIColor greenColor];

headerViewtag = section;

[_headerArray addObject:headerView];

return headerView;

}

- (void)scrollViewDidScroll:(UIScrollView )scrollView {

[self updateHeaderArray];

NSLog(@"------------");

for (UIView view in _headerArray) {

NSLog(@"visible section:%d", viewtag);

}

}

- (void)updateHeaderArray {

// remove invisible section headers

NSMutableArray removeArray = [NSMutableArray array];

CGRect containerRect = CGRectMake(_tableViewcontentOffsetx, _tableViewcontentOffsety,

_tableViewframesizewidth, _tableViewframesizeheight);

for (UIView header in _headerArray) {

if (!CGRectIntersectsRect(headerframe, containerRect)) {

[removeArray addObject:header];

}

}

[_headerArray removeObjectsInArray:removeArray];

}

7 答案是简单了很多,并用简洁KVC NSArray visibleSections = [selftableViewindexPathsForVisibleRows valueForKey:@"section"];

这可能给你重复的值的数组,但你可以从那里管理。

    /

      判断textview的字数占几行

      @param tv

      @return

     /

    private float measureTextLength(TextView tv)

    {

        if (paint == null)

        {

            paint = new Paint();

        }

        paintsetTextSize(tvgetTextSize());

        //计算textview的宽度,此处为屏幕宽度

        float tvW = mContextgetResources()getDisplayMetrics()widthPixels;

        return (paintmeasureText(tvgetText() + "") + 05f) / tvW;

    }

以上就是关于android中获取TextView位置全部的内容,包括:android中获取TextView位置、怎样获取uitableviewcell上uitextview的值、如何在 Android 的 textview 里获取行数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9303274.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存