- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 xml 中获取我的 TextView:
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:text="TextView"/>
我想创建多个 TextView,但我希望它们看起来与this 相同。
所以我尝试了:
TextView newTextView = new TextView(this);
newTextView.setLayoutParams(myTextView.getLayoutParams());
我认为这应该从 xml 直接(?)获取 myTextView 的所有布局参数,并将它们传递给 newTextView 以进行设置。
我的问题是:没有任何反应。没有生效,为什么?
最佳答案
这是一个示例项目,表明它可以正常工作。您可以通过查看可视化编辑器的预览看到它的工作原理,它看起来与运行时显示的不同。我认为你的错误是你没有为加权 View 设置 0px(或 0dp,零仍然是零)。
main.xml(布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/container">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="0px" android:text="TextView1"
android:layout_weight="1" android:background="#ffff0000" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView2"
android:background="#ff00ff00" />
</LinearLayout>
测试 Activity .java :
public class TestActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final TextView tv2=(TextView)findViewById(R.id.textView2);
final LayoutParams layoutParams=tv1.getLayoutParams();
tv2.setLayoutParams(layoutParams);
// adding textView programatically:
final TextView tv3=new TextView(this);
tv3.setText("textView3");
tv3.setBackgroundColor(0xff0000ff);
tv3.setLayoutParams(layoutParams);
final ViewGroup root=(ViewGroup)findViewById(R.id.container);
root.addView(tv3);
}
}
关于android - myView.setLayoutParams(otherView.getLayoutParams());不工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10762220/
我创建了一个扩展 View 的类: public class BoardView extends View { 并且我在应用程序的 main.xml 文件中指定了 BoardView 的宽度和高度:
我有一个自定义的 ImageView 类(我认为类中的代码并不重要,但无论如何我都会将其包含在这里以防万一): package com.example.vroy.customcirclebut
我试图在我的函数中获取 android:layout_weight。这是我的 xml 代码: ...... 我的功能部分: import android.widget.Li
如果我想在 Java 中以编程方式设置 ImageView 的宽度/高度,如下所示: image_view.getLayoutParams().width = 200; “200”的单位是什么?是px
我正在尝试以编程方式设置布局宽度。 ViewTreeObserver vtoRecyclerView = mMainLayout.getViewTreeObserver(); vto
例如,如果你想将某些 View 的 LayoutParams 更改为 WRAP_CONTENT,同时 width 和 height以编程方式,它看起来像这样: final ViewGroup.Lay
我已经编写了代码来动态设置 View 的高度和宽度 view.getLayoutParams().width = 120; view.getLayoutParams().height = 120; 它
View 的 getHeight() 和 getLayoutParams().height 有什么区别?我有一个 View (GoogleAdView),我想隐藏它,我将 getLayoutParam
我有以下看法: 我正在我的项目中实现一个计时器。因此,每秒调用一次函数 onTick(): public void onTick(long millisUntilFinished) {
我有一个 Seekbar,除了一个部分外,它工作得很好。我想根据 Seekbar 值调整 textView 的高度。 我正在使用这段代码来调整 textView 的高度: SeekBar sb1 =
我正在用这个简单的方法以编程方式创建一个小部件 private ImageView getModuleIconView(Module module) { ImageView view = ne
当我像这样在 RecyclerView.Adapter 中使用 ViewBinding 时: override fun onCreateViewHolder(parent: ViewGroup, vi
当我像这样在 RecyclerView.Adapter 中使用 ViewBinding 时: override fun onCreateViewHolder(parent: ViewGroup, vi
我有一个带有一个 View 对象(一个简单的位图)的RelativeLayout。为了兼容性,我使用 getLayoutParams() 和 setLayoutParams() 来移动它。 但是,当从
我有一个 RecyclerView,为此我有一个自定义适配器,并且我已将 LayoutManager 设置为 FlexboxLayoutManager。对于每个 child ,我想将 FlexGrow
如果我声明 LinearLayout linearLayout 并查看 linearLayout.getLayoutParams(),它会给我 ViewGroup.LayoutParams,而不是 L
在 xml 中获取我的 TextView: 我想创建多个 TextView,但我希望它们看起来与this 相同。 所以我尝试了: TextView newTextView = new TextVie
这是我创建新 ImageView 的源代码: ImageView image = new ImageView(this); LinearLayout.LayoutParams params =
本文整理了Java中com.google.android.youtube.player.YouTubeThumbnailView.getLayoutParams()方法的一些代码示例,展示了YouTu
我有一个 Android 对话框,我想将其放置在其窗口中的特定位置。 我正在使用 API 8 为什么int a == -2和int b == 153不是正数? 有什么区别 getLayoutParam
我是一名优秀的程序员,十分优秀!