我创建了我的应用程序,其高度和宽度以像素为单位,用于分辨率为480x800的Pantech设备.
我需要转换G1设备的高度和宽度.我认为将其转换为dp将解决问题,并为两种设备提供相同的解决方案.
有没有简单的方法将像素转换为dp?有什么建议?
我从不同的角度来看这个.基本上要点是:
我已经在XML中为一个以编程方式运行NEEDS的接口布局了一个模板,因此它将在运行期间动态填充.
这里的问题是,XML TextView有很多布局调整(有效),是必要的.但是如果我实际上在代码中设置它们,TextView甚至都不显示.
(顺便说一下,TextView嵌套在TableRow中,因此调用了权重.)我首先设计的XML模板,用作代码的参考是这样的,它工作得很好:
<TextView
android:id="@+id/txtviewWhiteBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/textview_9patch_white"
android:gravity="center"
android:text="75"
android:textColor="@android:color/black"
android:layout_margin="20dp"
android:padding="20dp"
android:textSize="40dp" />
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,完美地展示出来.当我在代码中运行相同的布局,并应用LayoutParams时,TextView消失.
这是相关的片段:
int textViewExampleID = 9001;
private TextView txtviewExample = new TextView(this);
private void buildTextView(){
LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
txtviewExample.setId(textViewExampleID);
txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
txtviewExample.setGravity(Gravity.CENTER);
txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
paramsExample.setMargins(20, 20, 20, 20);
txtviewExample.setPadding(20, 20, 20, 20);
txtviewExample.setTextSize(40);
txtviewExample.setText("customExample");
//if I comment out the following line, this TextView displays.
//if I leave it in, it doesn't display.
txtviewExample.setLayoutParams(paramsExample);
}
Run Code Online (Sandbox Code Playgroud)
我意识到LayoutParams有各种各样的可用类,我一直在玩它们所有的LinearLayout.LayoutParams,TableView.LayoutParams,RelativeLayout.LayoutParams,LayoutParams本身...无论我尝试哪一个,任何尝试调用"setLayoutParams"渲染整个TextView.我在这里搜索了论坛,但还没有找到答案.这不是那么罕见.
编辑:通过埃米尔的评论找到答案.代码最终如下:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
view.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
谢谢大家:D
我正在编写一个Android应用程序,我有一个动态创建的树,在其中显示ImageView元素.然而,为了安排这些图像,我使用了填充左边来正确排列它们,动态地改变填充的计算方式,以便一切都是均匀的.但是,当我这样做时,填充覆盖了左边的一些图像,即使你可以看到这个填充后面的图像,当你点击该图像时,它将评估正在使用填充的图像(最远的)左元素).我基本上想知道是否有一种方法可以以编程方式设置某些内容来执行padding对图像所做的但不可点击的内容?边距有意义,但您无法在ImageView上设置边距.
树设置如下:
-----A-----
--B--C--D--
-E-F----G--
Run Code Online (Sandbox Code Playgroud)
想象一下,每个字母都是一个ImageView,用padding动态放置.但是如果我点击B或C,D的代码将被评估,因为它的左侧填充覆盖了B和C.对于E或F,同样适用于那些,并且将对G进行评估.我无法想出一种不同的方式放置这些ImageViews.任何帮助是极大的赞赏.
我动态创建了很多按钮.如何在两个按钮之间添加空格.请帮我.提前致谢
我的代码:
private LinearLayout LLDynamic;
private RelativeLayout.LayoutParams ParaOne;
ParaOne = new RelativeLayout.LayoutParams(280, 30);
Button button1= new Button(Twindo_fromEditGroup.this);
button1.setTextSize(16);
LLDynamic = new LinearLayout(Twindo_fromEditGroup.this);
LLDynamic.setOrientation(LinearLayout.VERTICAL);
LLDynamic.addView(button1,ParaOne);
Run Code Online (Sandbox Code Playgroud) android ×4
button ×1
dpi ×1
dynamic ×1
layoutparams ×1
padding ×1
pixel ×1
resolution ×1
spaces ×1
textview ×1