使用代码设置图像布局(Android Studio)(Setting image layouts using code (Android Studio))
我有一个包含布局的活动。 我在此布局中设置了10个图像按钮,并隐藏(不可见)。 我试图以编程方式将图像加载到这些图像按钮,然后在屏幕上设置它们的位置。
我之前的讨论已经找到了: 如何使用代码设置ImageView的边距,而不是xml
但是,这不起作用。 尽管设置了高度和宽度参数,但我得到了微小的图像按钮。 这是我的代码:
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(btnGP1.getLayoutParams()); lp1.leftMargin = P1leftValue; lp1.topMargin = P1topValue; lp1.height = 80; lp1.width = 80; btnGP1.setLayoutParams(lp1);
关于这一点的事情是不对的,但我没有找到更准确的资源。 有任何想法吗?
更新:
我编写了我的代码:
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams)btnGP1.getLayoutParams(); lp1.leftMargin = P1leftValue; lp1.topMargin = P1topValue; lp1.height = 280; lp1.width = 280; btnGP1.setLayoutParams(lp1);
它似乎几乎正常工作。 现在唯一剩下的就是上面的函数似乎只允许我输入像素大小。 我需要能够输入像素密度,因为并非所有屏幕尺寸都相同。
我的新问题是:我该如何解决这个问题? 这里有一个简单的答案吗? 如果我尝试280dp或“280dp”,编译器将不允许它。
I have an activity, containing a layout. I have a pool of 10 imagebuttons set up and hidden (invisible) in this layout. I am trying to load the image to these imagebuttons programmatically, and then set their location on the screen.
I have found this previous discussion: How to set margin of ImageView using code, not xml
This, however, does not work. I am getting tiny imagebuttons as a result, despite setting the height and width parameters. Here is my code:
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(btnGP1.getLayoutParams()); lp1.leftMargin = P1leftValue; lp1.topMargin = P1topValue; lp1.height = 80; lp1.width = 80; btnGP1.setLayoutParams(lp1);
Something about this is not right, but I am not having any luck finding a more accurate resource. Any ideas?
Update:
I have edited my code to this:
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams)btnGP1.getLayoutParams(); lp1.leftMargin = P1leftValue; lp1.topMargin = P1topValue; lp1.height = 280; lp1.width = 280; btnGP1.setLayoutParams(lp1);
And it appears to be almost working. The only thing that remains now is that the functions above only appear to be allowing me to enter pixel sizes. I need to be able to enter pixel densities instead as not all screen sizes are the same.
My new question is this: how do I go about this? Is there a simple answer here? If I try 280dp or "280dp" the compiler will not allow it.
原文:https://stackoverflow.com/questions/30055314
最满意答案
我将
pd.read_fwf
用于固定宽度,但指定'\s{2,}'
的分隔符g = lambda x: '' if x.startswith('Unnamed') else x pd.read_fwf( 'file.txt', sep='\s{2,}', header=[0, 1, 2], skiprows=[3] ).rename(columns=g) OBJ OBJ DATE OBJ CLASS DATE OBJ CLASS ENTERED1 TAG REPL ENTERED2 NAME TSTMP USERID 0 EZ4 2002-02-06 D NaN 2002-02-06 abc random 2002-02-06-10.05.30.242768 2342342.0 1 000 2001-09-27 D NaN 2001-09-27 none 2001-09-27-10.23.31.121606 2343242.0 2 011 1974-07-09 NaN NaN 0001-01-01 board COMPENSATON 1997-01-22-09.10.23.145853 NaN
I'd use
pd.read_fwf
for fixed width but specify a separator of'\s{2,}'
g = lambda x: '' if x.startswith('Unnamed') else x pd.read_fwf( 'file.txt', sep='\s{2,}', header=[0, 1, 2], skiprows=[3] ).rename(columns=g) OBJ OBJ DATE OBJ CLASS DATE OBJ CLASS ENTERED1 TAG REPL ENTERED2 NAME TSTMP USERID 0 EZ4 2002-02-06 D NaN 2002-02-06 abc random 2002-02-06-10.05.30.242768 2342342.0 1 000 2001-09-27 D NaN 2001-09-27 none 2001-09-27-10.23.31.121606 2343242.0 2 011 1974-07-09 NaN NaN 0001-01-01 board COMPENSATON 1997-01-22-09.10.23.145853 NaN
相关问答
更多-
你可以逐行处理。 import re data = {} temp_s = match = '' with open('myfile.txt', 'r') as infile: for line in infile: if ":" in line: line = line.split(':') data[line[0]] = line[1] elif re.search(r'.*\w+', line ...
-
使用pandas读取包含多行标题的文本文件(Using pandas to read a text file with the headers in multiple rows)[2022-11-24]
我将pd.read_fwf用于固定宽度,但指定'\s{2,}'的分隔符 g = lambda x: '' if x.startswith('Unnamed') else x pd.read_fwf( 'file.txt', sep='\s{2,}', header=[0, 1, 2], skiprows=[3] ).rename(columns=g) OBJ OBJ ... -
如何将带有多个标题行的csv文件读入pandas?(How to read a csv file with multiple header rows into pandas?)[2022-02-27]
在read_csv需要参数header=[0,1] 。 You need parameter header=[0,1] in read_csv. -
您可以先使用不在数据中的分隔符read_csv ; ,然后用stack双split : import pandas as pd import numpy as np import io temp=u"""1=88|11=1438|15=KKK|45=7.7|45=00|21=66|86=a 4=13|4=1388|49=DDD|8=157.73|67=00|45=08|84=b|45=k 6=84|41=18|56=TTT|67=1.2|4=21|45=78|07=d """ #after testing ...
-
您可以在函数read_csv使用参数skiprows和header : import pandas as pd import io temp=u"""var1 var2 var3 var4 var5 var6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6""" print range(0, 10)[1::2] [1, 3, 5, 7, 9] #after testing replace io.String ...
-
熊猫读取undefined文本文件到dataframe [复制](pandas Read undelimited text file to dataframe [duplicate])[2022-11-13]
假设你的专栏被称为A 首先找到这样的状态: df.A.str.contains('\[edit\]') Out[25]: 0 True 1 False 2 False 3 False 4 False 5 False 6 False 7 False 8 False 9 True 10 False 11 True 12 False 13 False 14 False 使用cumsum为每个州+ ... -
尝试这个: In [33]: df = pd.read_csv(filename, comment='#', header=None, delim_whitespace=True) In [34]: df Out[34]: 0 1 2 3 4 5 6 7 0 101_DGCA3.1D[0] 101_LEC.1D[0] 0.85189 0.85071 0. ...
-
您可以使用read_csv并为设置新列名称添加参数名称。 然后你必须设置parse_dates=['c'] : import pandas as pd import io temp=u"""97ebd0c6680f7c0535dbfdead6e51b4b dd65fa250fca2833a3a8c16d2cf0457c ed180d7daf639d936f1aeae4f7fb482f 4725c39a5e5f4c188d382da3910b3f3f 3e12208dd0be281c92a6ab57 ...
-
解决方法是: import pandas as pd the_file = [] with open(r"./genes.txt", 'rb') as f: for line in f: the_file.append(line.split('\t')) df = pd.DataFrame(the_file,columns=range(max([len(l) for l in the_file]))) print df[0] 结果: 0 ...
-
使用pandas reader将文本文件读入numpy数组的问题(Issue with reading text file into numpy array using pandas reader)[2022-05-25]
如果separator是空格而header=None那么delim_whitespace=True似乎需要delim_whitespace=True : 然后施放到float : u=pd.read_csv('data.txt', delim_whitespace=True, header=None).astype(float).values print (u) [[ 1.44445500e+06 7.00000000e+00 8.00000000e+00 1.20000000e+01 ...