有没有办法避免Android布局上的大小规格?

时间:2014-01-09 15:39:20

标签: android android-layout

我每天使用Android,我想避免视图大小的规格。 例如,如果我创建TextView,我必须分配高度和宽度属性,如下所示:

   <TextView
      android:id="@+id/activity_lbl"
      android:layout_width="wrap_content" <!-- Again -->
      android:layout_height="wrap_content" <!--Again...  --> /> 

   <TextView
      android:id="@+id/activity_lbl2"
      android:layout_width="wrap_content" <!-- Again... -->
      android:layout_height="wrap_content" <!--Again, for ever  --> /> 

对于我创建的每个视图,我都要分配相同的大小规格。 有没有办法避免它们?

2 个答案:

答案 0 :(得分:5)

在您将在布局文件中应用的样式中声明它,如下所示:

文件:res / values / styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="wrapall">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
  </style>
</resources>

然后将其应用到您的布局中:

<TextView
      android:id="@+id/activity_lbl"
      style="@style/wrapall" /> 

   <TextView
      android:id="@+id/activity_lbl2"
      style="@style/wrapall" /> 

答案 1 :(得分:-2)

你无法避免

  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"

因为它很重要,但我会给你一些布局的代码 对于图像,您可以在课堂上使用它:

ImageView.getLayoutParams().height = YourHeightValue ;
    ImageView.getLayoutParams().width = YourWidthValue;
textView的

使用Textview.setSize(); or TextView.setwidth(int); TextView.setHeight(int);

相关问题